Refactor to use ai/rsc (#253)

This commit is contained in:
Jeremy 2024-03-14 20:00:52 +03:00 committed by GitHub
parent 69ca8fcc22
commit e85ba803dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
66 changed files with 2799 additions and 740 deletions

50
app/login/actions.ts Normal file
View file

@ -0,0 +1,50 @@
'use server'
import { signIn } from '@/auth'
import { AuthResult } from '@/lib/types'
import { AuthError } from 'next-auth'
import { z } from 'zod'
export async function authenticate(
_prevState: AuthResult | undefined,
formData: FormData
) {
try {
const email = formData.get('email')
const password = formData.get('password')
const parsedCredentials = z
.object({
email: z.string().email(),
password: z.string().min(6)
})
.safeParse({
email,
password
})
if (parsedCredentials.success) {
await signIn('credentials', {
email,
password,
redirectTo: '/'
})
} else {
return { type: 'error', message: 'Invalid credentials!' }
}
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return { type: 'error', message: 'Invalid credentials!' }
default:
return {
type: 'error',
message: 'Something went wrong, please try again!'
}
}
}
throw error
}
}