30 lines
745 B
TypeScript
30 lines
745 B
TypeScript
'use server';
|
|
|
|
import { signIn } from '@/auth';
|
|
import { AuthError } from 'next-auth';
|
|
|
|
export async function authenticate(
|
|
prevState: string | undefined,
|
|
formData: FormData,
|
|
) {
|
|
try {
|
|
await signIn('credentials', {
|
|
email: formData.get('email'),
|
|
password: formData.get('password'),
|
|
redirectTo: '/',
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
console.error('AuthError details:', error.type, error.message);
|
|
switch (error.type) {
|
|
case 'CredentialsSignin':
|
|
return 'Invalid credentials.';
|
|
default:
|
|
return `Auth error: ${error.type}`;
|
|
}
|
|
}
|
|
// IMPORTANT: Next.js redirects throw a special error that must be rethrown
|
|
throw error;
|
|
}
|
|
}
|