Added redemptions & redeemable actions. Fixed a few bugs.

This commit is contained in:
Tom
2024-06-24 22:16:55 +00:00
parent 68df045c54
commit 6548ce33e0
35 changed files with 1787 additions and 471 deletions

View File

@ -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 });