Chapter 15 - implement authentication

This commit is contained in:
2024-01-04 22:37:29 +01:00
parent 8e51988395
commit 045ba08b17
9 changed files with 280 additions and 52 deletions

View File

@@ -4,6 +4,9 @@ import { z } from 'zod';
import { sql } from '@vercel/postgres';
import { revalidatePath } from 'next/cache';
import {redirect} from "next/navigation";
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';
const FormSchema = z.object({
id: z.string(),
@@ -97,4 +100,23 @@ export async function deleteInvoice(id: string) {
} catch (error) {
return { message: 'Database Error: Failed to Delete Invoice.' };
}
}
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;
}
}