-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from devzero-inc/supabaseAuth
#1 PK: adds backend route for authentication using supabase
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |