Implemented email verification

This commit is contained in:
2024-04-12 13:52:16 +03:00
parent 78107d4ec7
commit b1ad7b5c3e
51 changed files with 604 additions and 213 deletions

View File

@@ -5,7 +5,7 @@ import bcrypt from 'bcryptjs'
import { RegisterSchema } from '@/schemas'
import { PASSWORD_SALT_LENGTH } from '@/config/validation'
import { db } from '@/lib/db'
import db from '@/lib/db'
import { getUserByEmail } from '@/data/user'
import { sendVerificationEmail } from '@/actions/send-verification-email'

View File

@@ -1,18 +1,16 @@
'use server'
import mailer from '@/lib/mailer'
import { env } from 'process'
import { AUTH_EMAIL_VERIFICATION_URL } from '@/config/routes'
import { AUTH_USER_VERIFICATION_URL } from '@/config/routes'
import { generateVerificationToken } from '@/lib/tokens'
import { env } from '@/lib/utils'
const sendVerificationEmail = async (email: string, name?: string | null) => {
const verificationToken = await generateVerificationToken(email)
const confirmLink: string = [env.SITE_URL, AUTH_EMAIL_VERIFICATION_URL, '?token=', verificationToken].join('')
const confirmLink: string = [env('SITE_URL'), AUTH_USER_VERIFICATION_URL, verificationToken.token].join('')
const { isOk, code, info, error } = await mailer({
to: name ? [
{ name: name?.toString(), address: verificationToken.email },
`test-xyhy2bvhj@srv1.mail-tester.com`] : verificationToken.email,
to: name ? { name: name?.toString(), address: verificationToken.email } : verificationToken.email,
subject: 'Complete email verification for A-Naklejka',
html: `<p>Click <a href="${confirmLink}">here</a> to confirm email</p>`,
})
@@ -20,7 +18,7 @@ const sendVerificationEmail = async (email: string, name?: string | null) => {
if (isOk) {
return { success: code === 250 ? 'auth.email.success.confirmation_email_sent' : info?.response }
} else {
return { error: env.DEBUG === 'true' ? error?.response : 'auth.email.error.verification_email_sending_error' }
return { error: env('DEBUG') === 'true' ? error?.response : 'auth.email.error.verification_email_sending_error' }
}
}

View File

@@ -0,0 +1,41 @@
'use server'
import db from '@/lib/db'
import { getVerificationTokenByToken } from '@/data/verification-token'
import { getUserByEmail } from '@/data/user'
export const userVerification = async (token: string) => {
const existingToken = await getVerificationTokenByToken(token)
if (!existingToken) return { error: 'No verification token found!' }
const tokenHasExpired: boolean = new Date(existingToken.expires) < new Date()
if (tokenHasExpired) return { error: 'Unfortunately your token has expired!' }
const existingUser = await getUserByEmail(existingToken.email)
if (!existingUser) return { error: 'Email associated with token not found!' }
try {
await db.user.update({
where: { id: existingUser.id }, data: {
email: existingToken.email, emailVerified: new Date(),
},
})
} catch (e) {
console.error(e)
return { error: 'Could not update user data! Please, try again by reloading page!' }
}
try {
await db.verificationToken.delete({
where: { id: existingToken.id },
})
} catch (e) {
// TODO: log error on disc or db
console.error(e)
}
return { success: 'User verified!' }
}