Added redemptions & redeemable actions. Fixed a few bugs.
This commit is contained in:
		@@ -1,22 +1,20 @@
 | 
			
		||||
import axios from 'axios'
 | 
			
		||||
import { db } from "@/lib/db"
 | 
			
		||||
import { NextResponse } from "next/server";
 | 
			
		||||
import fetchUser from '@/lib/fetch-user';
 | 
			
		||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
 | 
			
		||||
 | 
			
		||||
export async function GET(req: Request) {
 | 
			
		||||
    try {
 | 
			
		||||
        // Verify state against user id in user table.
 | 
			
		||||
        const key = await db.apiKey.findFirst({
 | 
			
		||||
          where: {
 | 
			
		||||
            id: req.headers.get('x-api-key') as string
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
        if (!key) {
 | 
			
		||||
            return new NextResponse("Forbidden", { status: 403 });
 | 
			
		||||
        const user = await fetchUserWithImpersonation(req)
 | 
			
		||||
        if (!user) {
 | 
			
		||||
            return new NextResponse("Unauthorized", { status: 401 });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const connection = await db.twitchConnection.findFirst({
 | 
			
		||||
          where: {
 | 
			
		||||
            userId: key.userId
 | 
			
		||||
            userId: user.id
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
        if (!connection) {
 | 
			
		||||
@@ -29,8 +27,22 @@ export async function GET(req: Request) {
 | 
			
		||||
                Authorization: 'OAuth ' + connection.accessToken
 | 
			
		||||
              }
 | 
			
		||||
            })).data;
 | 
			
		||||
            if (expires_in > 3600)
 | 
			
		||||
                return new NextResponse("", { status: 201 });
 | 
			
		||||
 | 
			
		||||
            if (expires_in > 3600) {
 | 
			
		||||
                let data = await db.twitchConnection.findFirst({
 | 
			
		||||
                    where: {
 | 
			
		||||
                      userId: user.id
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
 | 
			
		||||
                let dataFormatted = {
 | 
			
		||||
                    user_id: user.id,
 | 
			
		||||
                    access_token: data?.accessToken,
 | 
			
		||||
                    refresh_token: data?.refreshToken,
 | 
			
		||||
                    broadcaster_id: connection.broadcasterId
 | 
			
		||||
                }
 | 
			
		||||
                return NextResponse.json(dataFormatted, { status: 201 });
 | 
			
		||||
            }
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -51,14 +63,22 @@ export async function GET(req: Request) {
 | 
			
		||||
 | 
			
		||||
        await db.twitchConnection.update({
 | 
			
		||||
          where: {
 | 
			
		||||
            userId: key.userId
 | 
			
		||||
            userId: user.id
 | 
			
		||||
          },
 | 
			
		||||
          data: {
 | 
			
		||||
            accessToken: access_token
 | 
			
		||||
            accessToken: access_token,
 | 
			
		||||
            refreshToken: refresh_token
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        const data = {
 | 
			
		||||
            user_id: user.id,
 | 
			
		||||
            access_token,
 | 
			
		||||
            refresh_token,
 | 
			
		||||
            broadcaster_id: connection.broadcasterId
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        return new NextResponse("", { status: 200 });
 | 
			
		||||
        return NextResponse.json(data)
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.log("[ACCOUNT]", error);
 | 
			
		||||
        return new NextResponse("Internal Error", { status: 500 });
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								app/api/account/redemptions/route.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/api/account/redemptions/route.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
import { db } from "@/lib/db"
 | 
			
		||||
import { NextResponse } from "next/server";
 | 
			
		||||
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
 | 
			
		||||
import axios from "axios";
 | 
			
		||||
import { updateTwitchToken } from "@/data/twitch-reauthorize";
 | 
			
		||||
 | 
			
		||||
export async function GET(req: Request) {
 | 
			
		||||
    try {
 | 
			
		||||
        if (!process.env.TWITCH_BOT_CLIENT_ID)
 | 
			
		||||
            return new NextResponse("Internal Error", { status: 500 });
 | 
			
		||||
 | 
			
		||||
        const user = await fetchUserWithImpersonation(req)
 | 
			
		||||
        if (!user) {
 | 
			
		||||
            return new NextResponse("Unauthorized", { status: 401 });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const auth = await updateTwitchToken(user.id)
 | 
			
		||||
        if (!auth)
 | 
			
		||||
            return new NextResponse("Bad Request", { status: 400 })
 | 
			
		||||
 | 
			
		||||
        const redemptions = await axios.get("https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=" + auth.broadcaster_id,
 | 
			
		||||
            {
 | 
			
		||||
                headers: {
 | 
			
		||||
                    "Client-Id": process.env.TWITCH_BOT_CLIENT_ID,
 | 
			
		||||
                    "Authorization": "Bearer " + auth.access_token
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        return NextResponse.json(redemptions.data);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.log("[REDEMPTIONS/ACTIONS]", error);
 | 
			
		||||
        return new NextResponse("Internal Error", { status: 500 });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,7 +6,9 @@ import fetchUser from "@/lib/fetch-user";
 | 
			
		||||
 | 
			
		||||
export async function GET(req: Request) {
 | 
			
		||||
    try {
 | 
			
		||||
      return NextResponse.json(await fetchUser(req))
 | 
			
		||||
      const user = await fetchUser(req)
 | 
			
		||||
      if (!user) return new NextResponse("Internal Error", { status: 401 })
 | 
			
		||||
      return NextResponse.json(user)
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
        console.log("[ACCOUNT]", error);
 | 
			
		||||
        return new NextResponse("Internal Error", { status: 500 });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user