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

@@ -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!' }
}