hermes-web/app/api/account/route.ts

59 lines
1.7 KiB
TypeScript

import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import { auth } from "@/auth";
import fetchUser from "@/lib/fetch-user";
export async function GET(req: Request) {
try {
const user = await fetchUser(req)
if (!user) return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 })
const account = await db.account.findFirst({
where: {
userId: user.id
}
});
return NextResponse.json({ ... user, broadcasterId: account?.providerAccountId })
} catch (error) {
console.log("[ACCOUNT]", error);
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
export async function POST(req: Request) {
try {
const session = await auth()
const user = session?.user?.name
if (!user) {
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 })
}
const exist = await db.user.findFirst({
where: {
name: user
}
});
if (exist) {
return NextResponse.json({
id: exist.id,
username: exist.name
});
}
const newUser = await db.user.create({
data: {
name: user,
}
});
return NextResponse.json({
id: newUser.id,
username: newUser.name
});
} catch (error) {
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}