Added basic validation for requests
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
// TODO: remove this page.
|
||||
|
||||
import axios from 'axios'
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
@ -10,7 +12,7 @@ export async function GET(req: Request) {
|
||||
const state = searchParams.get('state') as string
|
||||
|
||||
if (!code || !scope || !state) {
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'Missing oauth2 data.', error: null, value: null }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify state against user id in user table.
|
||||
@ -21,7 +23,7 @@ export async function GET(req: Request) {
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'You do not have permissions for this.', error: null, value: null }, { status: 403 });
|
||||
}
|
||||
|
||||
// Post to https://id.twitch.tv/oauth2/token
|
||||
@ -37,7 +39,7 @@ export async function GET(req: Request) {
|
||||
const { access_token, expires_in, refresh_token, token_type } = token
|
||||
|
||||
if (!access_token || !refresh_token || token_type !== "bearer") {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
||||
}
|
||||
|
||||
let info = await axios.get("https://api.twitch.tv/helix/users?login=" + user.name, {
|
||||
@ -57,9 +59,9 @@ export async function GET(req: Request) {
|
||||
}
|
||||
})
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
|
||||
} catch (error) {
|
||||
console.log("[ACCOUNT/AUTHORIZE]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user