diff --git a/roadmap-voting-app/app/api/user/route.ts b/roadmap-voting-app/app/api/user/route.ts new file mode 100644 index 0000000..f4f328c --- /dev/null +++ b/roadmap-voting-app/app/api/user/route.ts @@ -0,0 +1,41 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { supabase } from '../../../lib/supabaseClient'; + +export async function POST(req: NextRequest) { + try { + const login = req.nextUrl.searchParams.get("login") as string; + const formdata = await req.formData(); + const email = formdata.get("email") as string; + const password = formdata.get("password") as string; + if (login) { + const { data , error } = await supabase.auth.signInWithPassword({ email, password }); + if (error) throw error; + return NextResponse.json({ message: "User logged in successfully", data: data, status: 200 }); + } else { + const username = formdata.get("username") as string; + + console.log(username, email, password); + if (!username) { + return NextResponse.json({ message: 'Username is required', status: 400 }); + } + if (!email) { + return NextResponse.json({ message: 'Email is required', status: 400 }); + } + if (!password) { + return NextResponse.json({ message: 'Password is required', status: 400 }); + } + + const signUpResponse = await supabase.auth.signUp({ email, password }); + if (signUpResponse.error) throw signUpResponse.error; + + return NextResponse.json({ message: "user created successfuly", data:signUpResponse.data , status: 200 }); + } + } catch (error) { + // console.log(error); + if (error instanceof Error) { + return NextResponse.json({ message: error.message, status: 401 }); + } else { + return NextResponse.json({ message: 'An unexpected error occurred', status: 500 }); + } + } +} \ No newline at end of file diff --git a/roadmap-voting-app/lib/supabaseClient.ts b/roadmap-voting-app/lib/supabaseClient.ts new file mode 100644 index 0000000..cd190dd --- /dev/null +++ b/roadmap-voting-app/lib/supabaseClient.ts @@ -0,0 +1,6 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL as string; +const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY as string; + +export const supabase = createClient(supabaseUrl, supabaseAnonKey); \ No newline at end of file