60 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.5 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 new NextResponse("Internal Error", { 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 new NextResponse("Internal Error", { status: 500 });
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
export async function POST(req: Request) {
 | 
						|
    try {
 | 
						|
        const session = await auth()
 | 
						|
        const user = session?.user?.name
 | 
						|
        if (!user) {
 | 
						|
            return new NextResponse("Internal Error", { 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) {
 | 
						|
        console.log("[ACCOUNT]", error);
 | 
						|
        return new NextResponse("Internal Error", { status: 500 });
 | 
						|
    }
 | 
						|
} |