diff --git a/src/app/api/rest/v1/isUsername/route.ts b/src/app/api/rest/v1/isUsername/route.ts index 1be0ff4..6e60734 100644 --- a/src/app/api/rest/v1/isUsername/route.ts +++ b/src/app/api/rest/v1/isUsername/route.ts @@ -1,29 +1,25 @@ import {NextRequest, NextResponse} from "next/server"; +import { createClient } from "@/utils/supabase/server"; export async function POST(req: NextRequest): Promise { - let response = NextResponse.next({ - request: { - headers: req.headers, - }, - }) + try { + const supabase = createClient(); + const body = await req.json(); + if (!body.username) { + return NextResponse.json({ error: 'username is required' }, { status: 400 }); + } - const body = await req.json() + const { data, error } = await supabase + .rpc('is_username_exist', + { username: body.username } + ); - if ( !body.username ) { - return NextResponse.json({ error: 'username is required' }) - } + if (error) { + return NextResponse.json({ error: error.message }, { status: 500 }); + } - let res = await fetch(`${process.env.NEXT_PUBLIC_SUPABASE_URL}/rest/v1/rpc/is_username_exist`, { - method: 'POST', - headers: { - "apikey": process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, - "Authorization": `Bearer ${process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!}`, - "Content-Type": "application/json", - }, - body: JSON.stringify(body) - }); - - let data = await res.json(); - - return NextResponse.json({ state: data }); + return NextResponse.json({ state: data }, { status: 200 }); + } catch (error: any) { + return NextResponse.json({ error: error.message }, { status: 500 }); + } } \ No newline at end of file