2024-08-25 17:35:46 -04:00
|
|
|
// TODO: remove this page.
|
|
|
|
|
2023-12-30 05:56:40 -05:00
|
|
|
import axios from 'axios'
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
|
|
const code = searchParams.get('code') as string
|
|
|
|
const scope = searchParams.get('scope') as string
|
|
|
|
const state = searchParams.get('state') as string
|
|
|
|
|
|
|
|
if (!code || !scope || !state) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Missing oauth2 data.', error: null, value: null }, { status: 400 });
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
2023-12-31 05:41:55 -05:00
|
|
|
|
2023-12-30 05:56:40 -05:00
|
|
|
// Verify state against user id in user table.
|
|
|
|
const user = await db.user.findFirst({
|
2024-08-14 16:33:40 -04:00
|
|
|
where: {
|
|
|
|
id: state
|
|
|
|
}
|
2023-12-30 05:56:40 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!user) {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'You do not have permissions for this.', error: null, value: null }, { status: 403 });
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post to https://id.twitch.tv/oauth2/token
|
2024-08-14 16:33:40 -04:00
|
|
|
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", {
|
2023-12-30 05:56:40 -05:00
|
|
|
client_id: process.env.TWITCH_BOT_CLIENT_ID,
|
|
|
|
client_secret: process.env.TWITCH_BOT_CLIENT_SECRET,
|
|
|
|
code: code,
|
|
|
|
grant_type: "authorization_code",
|
2024-08-14 16:33:40 -04:00
|
|
|
redirect_uri: "https://tomtospeech.com/api/account/authorize"
|
2023-12-30 05:56:40 -05:00
|
|
|
})).data
|
|
|
|
|
|
|
|
// Fetch values from token.
|
|
|
|
const { access_token, expires_in, refresh_token, token_type } = token
|
|
|
|
|
|
|
|
if (!access_token || !refresh_token || token_type !== "bearer") {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
|
2024-01-02 02:26:20 -05:00
|
|
|
let info = await axios.get("https://api.twitch.tv/helix/users?login=" + user.name, {
|
2023-12-30 05:56:40 -05:00
|
|
|
headers: {
|
|
|
|
"Authorization": "Bearer " + access_token,
|
|
|
|
"Client-Id": process.env.TWITCH_BOT_CLIENT_ID
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const broadcasterId = info.data.data[0]['id']
|
|
|
|
|
|
|
|
await db.twitchConnection.create({
|
2024-08-14 16:33:40 -04:00
|
|
|
data: {
|
|
|
|
broadcasterId: broadcasterId,
|
|
|
|
accessToken: access_token,
|
|
|
|
refreshToken: refresh_token,
|
|
|
|
userId: state
|
|
|
|
}
|
2023-12-30 05:56:40 -05:00
|
|
|
})
|
2024-08-14 16:33:40 -04:00
|
|
|
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
|
2023-12-30 05:56:40 -05:00
|
|
|
} catch (error) {
|
2024-08-14 16:33:40 -04:00
|
|
|
console.log("[ACCOUNT/AUTHORIZE]", error);
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2023-12-30 05:56:40 -05:00
|
|
|
}
|
|
|
|
}
|