2023-12-30 10:56:40 +00:00
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
2024-01-02 07:26:20 +00:00
|
|
|
import { auth } from "@/auth";
|
2024-01-04 21:57:32 +00:00
|
|
|
import fetchUser from "@/lib/fetch-user";
|
2023-12-30 10:56:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
2024-08-14 20:33:40 +00:00
|
|
|
const user = await fetchUser(req)
|
2024-08-25 21:35:46 +00:00
|
|
|
if (!user) return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 })
|
2024-08-14 20:33:40 +00:00
|
|
|
|
|
|
|
const account = await db.account.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return NextResponse.json({ ... user, broadcasterId: account?.providerAccountId })
|
2023-12-30 10:56:40 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("[ACCOUNT]", error);
|
2024-08-25 21:35:46 +00:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2023-12-30 10:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function POST(req: Request) {
|
|
|
|
try {
|
2024-01-02 07:26:20 +00:00
|
|
|
const session = await auth()
|
2023-12-30 10:56:40 +00:00
|
|
|
const user = session?.user?.name
|
|
|
|
if (!user) {
|
2024-08-25 21:35:46 +00:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 })
|
2023-12-30 10:56:40 +00:00
|
|
|
}
|
2024-08-14 20:33:40 +00:00
|
|
|
|
2023-12-30 10:56:40 +00:00
|
|
|
const exist = await db.user.findFirst({
|
2024-08-14 20:33:40 +00:00
|
|
|
where: {
|
|
|
|
name: user
|
|
|
|
}
|
2023-12-30 10:56:40 +00:00
|
|
|
});
|
2024-08-14 20:33:40 +00:00
|
|
|
|
2023-12-30 10:56:40 +00:00
|
|
|
if (exist) {
|
2023-12-31 10:41:55 +00:00
|
|
|
return NextResponse.json({
|
2024-08-14 20:33:40 +00:00
|
|
|
id: exist.id,
|
|
|
|
username: exist.name
|
2023-12-31 10:41:55 +00:00
|
|
|
});
|
2023-12-30 10:56:40 +00:00
|
|
|
}
|
2024-08-14 20:33:40 +00:00
|
|
|
|
2023-12-30 10:56:40 +00:00
|
|
|
const newUser = await db.user.create({
|
2024-08-14 20:33:40 +00:00
|
|
|
data: {
|
|
|
|
name: user,
|
|
|
|
}
|
2023-12-30 10:56:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return NextResponse.json({
|
2024-08-14 20:33:40 +00:00
|
|
|
id: newUser.id,
|
|
|
|
username: newUser.name
|
2023-12-30 10:56:40 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-08-25 21:35:46 +00:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2023-12-30 10:56:40 +00:00
|
|
|
}
|
|
|
|
}
|