added mail service
This commit is contained in:
55
actions/login.ts
Normal file
55
actions/login.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
'use server'
|
||||
|
||||
import { infer as zInfer } from 'zod'
|
||||
import { LoginSchema } from '@/schemas'
|
||||
import { signIn } from '@/config/auth'
|
||||
import { DEFAULT_LOGIN_REDIRECT } from '@/config/routes'
|
||||
import { AuthError } from 'next-auth'
|
||||
import { getUserByEmail } from '@/data/user'
|
||||
import { sendVerificationEmail } from '@/actions/send-verification-email'
|
||||
|
||||
export const login = async (values: zInfer<typeof LoginSchema>) => {
|
||||
const validatedFields = LoginSchema.safeParse(values)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return { error: 'auth.form.error.invalid_fields' }
|
||||
}
|
||||
|
||||
const { email, password } = validatedFields.data
|
||||
|
||||
const existingUser = await getUserByEmail(email)
|
||||
|
||||
if (!existingUser || !existingUser.email || !existingUser.password) {
|
||||
return { error: 'auth.form.error.invalid_credentials' }
|
||||
}
|
||||
|
||||
if (!existingUser.emailVerified) {
|
||||
return await sendVerificationEmail(existingUser.email, existingUser.name)
|
||||
}
|
||||
|
||||
try {
|
||||
await signIn('credentials', {
|
||||
email, password, redirectTo: DEFAULT_LOGIN_REDIRECT,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
switch (error.type) {
|
||||
case 'CredentialsSignin':
|
||||
return { error: 'auth.form.error.invalid_credentials' }
|
||||
case 'AccessDenied':
|
||||
return { error: 'auth.form.error.access_denied' }
|
||||
default:
|
||||
console.error('ERROR.TYPE:', error.type)
|
||||
return { error: 'common.something_went_wrong' }
|
||||
}
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export const SignInProvider = async (provider: 'google' | 'github' | 'facebook') => {
|
||||
await signIn(provider, {
|
||||
redirectTo: DEFAULT_LOGIN_REDIRECT,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user