sipher/src/app/api/auth/login/route.ts
Nixyi eef9803249 Error Handling
Made some error handling on some routes to prevent empty strings to be passed to the DB function.
2024-12-13 09:16:04 -03:00

49 lines
No EOL
1.2 KiB
TypeScript

// app/api/auth/login/route.ts
import {createClient} from "@/lib/supabase/server";
import {NextResponse} from "next/server";
export async function POST(request: Request) {
try {
const {username, password} = await request.json()
const supabase = await createClient()
const domain = process.env.DOMAIN;
if (!domain) {
return NextResponse.json({
error: "Server is misconfigured, please check env variables and try again."
},
{
status: 500
})
}
// Mocks the email with the domain we configured on the local env
const email = `${username.toLowerCase()}@${domain}`
// Sends the request through supabase
const {data: {user}, error: authError} = await supabase.auth.signInWithPassword({
email: email,
password: password,
})
if (authError) throw authError
// Fetch our custom user data
const {data: userData, error: userError} = await supabase
.from('users')
.select('*')
.eq('uuid', user?.id)
.single()
if (userError) throw userError
// Returns simple data
return NextResponse.json({user: userData})
} catch (error) {
return NextResponse.json(
{error: `Login failed: ${error}`},
{status: 401}
)
}
}