Upgraded to Next Auth v5

This commit is contained in:
Tom
2024-01-02 07:26:20 +00:00
parent a3352af981
commit 4505654a05
24 changed files with 283 additions and 227 deletions

View File

@ -9,9 +9,6 @@ export async function GET(req: Request) {
const scope = searchParams.get('scope') as string
const state = searchParams.get('state') as string
console.log("CODE:", code)
console.log("SCOPE:", scope)
console.log("STATE:", state)
if (!code || !scope || !state) {
return new NextResponse("Bad Request", { status: 400 });
}
@ -38,21 +35,17 @@ export async function GET(req: Request) {
// Fetch values from token.
const { access_token, expires_in, refresh_token, token_type } = token
// console.log("AT", access_token)
// console.log("RT", refresh_token)
// console.log("TT", token_type)
if (!access_token || !refresh_token || token_type !== "bearer") {
return new NextResponse("Unauthorized", { status: 401 });
}
let info = await axios.get("https://api.twitch.tv/helix/users?login=" + user.username, {
let info = await axios.get("https://api.twitch.tv/helix/users?login=" + user.name, {
headers: {
"Authorization": "Bearer " + access_token,
"Client-Id": process.env.TWITCH_BOT_CLIENT_ID
}
})
console.log(info.data.data)
const broadcasterId = info.data.data[0]['id']
await db.twitchConnection.create({

View File

@ -1,7 +1,6 @@
import axios from 'axios'
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import { GET as authorize } from '../authorize/route'
export async function GET(req: Request) {
try {
@ -11,7 +10,6 @@ export async function GET(req: Request) {
id: req.headers.get('x-api-key') as string
}
})
if (!key) {
return new NextResponse("Forbidden", { status: 403 });
}
@ -46,9 +44,6 @@ export async function GET(req: Request) {
// Fetch values from token.
const { access_token, expires_in, refresh_token, token_type } = token
// console.log("AT", access_token)
// console.log("RT", refresh_token)
// console.log("TT", token_type)
if (!access_token || !refresh_token || token_type !== "bearer") {
return new NextResponse("Unauthorized", { status: 401 });

View File

@ -1,7 +1,6 @@
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { generateToken } from "../token/route";
import { auth } from "@/auth";
import fetchUserUsingAPI from "@/lib/validate-api";
@ -16,7 +15,7 @@ export async function GET(req: Request) {
export async function POST(req: Request) {
try {
const session = await getServerSession()
const session = await auth()
const user = session?.user?.name
if (!user) {
return new NextResponse("Internal Error", { status: 401 })
@ -24,26 +23,26 @@ export async function POST(req: Request) {
const exist = await db.user.findFirst({
where: {
username: user.toLowerCase() as string
name: user
}
});
if (exist) {
return NextResponse.json({
id: exist.id,
username: exist.username
username: exist.name
});
}
const newUser = await db.user.create({
data: {
username: user.toLowerCase() as string,
name: user,
}
});
return NextResponse.json({
id: newUser.id,
username: newUser.username
username: newUser.name
});
} catch (error) {
console.log("[ACCOUNT]", error);

View File

@ -1,40 +0,0 @@
import type { NextAuthOptions } from "next-auth";
import TwitchProvider from "next-auth/providers/twitch";
export interface TwitchProfile extends Record<string, any> {
sub: string
preferred_username: string
email: string
picture: string
}
export const options: NextAuthOptions = {
providers: [
TwitchProvider({
clientId: process.env.TWITCH_CLIENT_ID as string,
clientSecret: process.env.TWITCH_CLIENT_SECRET as string,
authorization: {
params: {
scope: "openid user:read:email",
claims: {
id_token: {
email: null,
picture: null,
preferred_username: null,
},
},
},
},
idToken: true,
profile(profile) {
return {
id: profile.sub,
name: profile.preferred_username,
email: profile.email,
image: profile.picture,
}
},
})
],
secret: process.env.NEXTAUTH_SECRET
}

View File

@ -1,6 +1 @@
import NextAuth from 'next-auth'
import { options } from './options'
const handler = NextAuth(options)
export { handler as GET, handler as POST }
export { GET, POST } from "@/auth"

View File

@ -1,4 +1,3 @@
import axios from "axios"
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import fetchUserUsingAPI from "@/lib/validate-api";
@ -7,7 +6,6 @@ export async function GET(req: Request) {
try {
const user = await fetchUserUsingAPI(req)
if (!user) {
console.log("TWITCH CONNECT", user)
return new NextResponse("Unauthorized", { status: 401 });
}
@ -26,56 +24,6 @@ export async function GET(req: Request) {
}
});
return NextResponse.json(connection);
} catch (error) {
console.log("[CONNECTION/TWITCH]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function POST(req: Request) {
try {
const { id, secret } = await req.json();
const user = await fetchUserUsingAPI(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
}
let response = null;
try {
response = await axios.post("https://id.twitch.tv/oauth2/token", {
client_id: id,
client_secret: secret,
grant_type: "client_credentials"
});
console.log(response.data)
} catch (error) {
console.log("[CONNECTIONS/TWITCH/TOKEN]", error);
return;
}
console.log(user.username)
let info = await axios.get("https://api.twitch.tv/helix/users?login=" + user.username, {
headers: {
"Authorization": "Bearer " + response.data['access_token'],
"Client-Id": id
}
})
console.log(info.data.data)
const broadcasterId = info.data.data[0]['id']
const username = info.data.data[0]['login']
const connection = await db.twitchConnection.create({
data: {
id: id,
secret,
userId: user.id as string,
broadcasterId,
username
}
});
return NextResponse.json(connection);
} catch (error) {
console.log("[CONNECTION/TWITCH]", error);

View File

@ -14,7 +14,6 @@ export async function GET(req: Request) {
userId: user.id
}
})
if (!api) {
return new NextResponse("Forbidden", { status: 403 });
}

View File

@ -1,22 +0,0 @@
import { db } from "@/lib/db"
import { NextResponse } from "next/server";
export async function GET(req: Request, { params } : { params: { id: string } }) {
try {
let id = req.headers.get('x-api-key')
if (id == null) {
return NextResponse.json(null);
}
const tokens = await db.apiKey.findFirst({
where: {
id: id as string
}
});
return NextResponse.json(tokens);
} catch (error) {
console.log("[VALIDATE/GET]", error);
return new NextResponse("Internal Error", { status: 500});
}
}