2023-12-30 05:56:40 -05:00
|
|
|
import axios from 'axios'
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
2024-06-24 18:16:55 -04:00
|
|
|
import fetchUser from '@/lib/fetch-user';
|
|
|
|
import fetchUserWithImpersonation from '@/lib/fetch-user-impersonation';
|
2023-12-30 05:56:40 -05:00
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
|
|
|
// Verify state against user id in user table.
|
2024-06-24 18:16:55 -04:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
|
|
|
if (!user) {
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const connection = await db.twitchConnection.findFirst({
|
|
|
|
where: {
|
2024-06-24 18:16:55 -04:00
|
|
|
userId: user.id
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if (!connection) {
|
|
|
|
return new NextResponse("Forbidden", { status: 403 });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { expires_in }: { client_id:string, login:string, scopes:string[], user_id:string, expires_in:number } = (await axios.get("https://id.twitch.tv/oauth2/validate", {
|
|
|
|
headers: {
|
|
|
|
Authorization: 'OAuth ' + connection.accessToken
|
|
|
|
}
|
|
|
|
})).data;
|
2024-06-24 18:16:55 -04:00
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
2023-12-30 05:56:40 -05:00
|
|
|
} catch (error) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post to https://id.twitch.tv/oauth2/token
|
|
|
|
const token: { access_token:string, expires_in:number, refresh_token:string, token_type:string, scope:string[] } = (await axios.post("https://id.twitch.tv/oauth2/token", {
|
|
|
|
client_id: process.env.TWITCH_BOT_CLIENT_ID,
|
|
|
|
client_secret: process.env.TWITCH_BOT_CLIENT_SECRET,
|
|
|
|
grant_type: "refresh_token",
|
|
|
|
refresh_token: connection.refreshToken
|
|
|
|
})).data
|
|
|
|
|
|
|
|
// Fetch values from token.
|
|
|
|
const { access_token, expires_in, refresh_token, token_type } = token
|
|
|
|
|
|
|
|
if (!access_token || !refresh_token || token_type !== "bearer") {
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.twitchConnection.update({
|
|
|
|
where: {
|
2024-06-24 18:16:55 -04:00
|
|
|
userId: user.id
|
2023-12-30 05:56:40 -05:00
|
|
|
},
|
|
|
|
data: {
|
2024-06-24 18:16:55 -04:00
|
|
|
accessToken: access_token,
|
|
|
|
refreshToken: refresh_token
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
})
|
2024-06-24 18:16:55 -04:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
user_id: user.id,
|
|
|
|
access_token,
|
|
|
|
refresh_token,
|
|
|
|
broadcaster_id: connection.broadcasterId
|
|
|
|
}
|
2023-12-30 05:56:40 -05:00
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
return NextResponse.json(data)
|
2023-12-30 05:56:40 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.log("[ACCOUNT]", error);
|
|
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
|
|
}
|
|
|
|
}
|