Refactor to use ai/rsc (#253)
This commit is contained in:
parent
69ca8fcc22
commit
e85ba803dd
66 changed files with 2799 additions and 740 deletions
50
app/login/actions.ts
Normal file
50
app/login/actions.ts
Normal 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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue