Skip to content

Commit

Permalink
Merge pull request #10 from devzero-inc/supabaseAuth
Browse files Browse the repository at this point in the history
#1 PK: adds backend route for authentication using supabase
  • Loading branch information
AdoshSingh authored Feb 8, 2024
2 parents a016e4c + 2fdbc35 commit 999ca15
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
41 changes: 41 additions & 0 deletions roadmap-voting-app/app/api/user/route.ts
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 });
}
}
}
6 changes: 6 additions & 0 deletions roadmap-voting-app/lib/supabaseClient.ts
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);

0 comments on commit 999ca15

Please sign in to comment.