mirror of
https://github.com/michivonah/nextjs.git
synced 2025-12-22 22:16:28 +01:00
add authentication with next-auth
This commit is contained in:
parent
5487bccd8d
commit
759acde045
11 changed files with 318 additions and 98 deletions
|
|
@ -4,9 +4,30 @@ import { z } from 'zod';
|
|||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import postgres from 'postgres';
|
||||
import { signIn } from '@/auth';
|
||||
import { AuthError } from 'next-auth';
|
||||
|
||||
const sql = postgres(process.env.POSTGRES_URL!, {ssl: 'require'});
|
||||
|
||||
export async function authenticate(
|
||||
prevState: string | undefined,
|
||||
formData: FormData,
|
||||
){
|
||||
try{
|
||||
await signIn('credentials', formData)
|
||||
} catch(error){
|
||||
if(error instanceof AuthError){
|
||||
switch(error.type){
|
||||
case 'CredentialsSignIn':
|
||||
return 'Invalid credentials.';
|
||||
default:
|
||||
return 'Something went wrong.';
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const FormSchema = z.object({
|
||||
id: z.string(),
|
||||
customerId: z.string({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue