changed a lot

This commit is contained in:
Harivansh Rathi 2024-11-25 01:24:37 -05:00
parent ef9ccf22d3
commit 28901128ff
20 changed files with 1794 additions and 526 deletions

View file

@ -2,40 +2,47 @@
import { db } from '@/lib/db'
import { getUserByEmail } from '@/data/user'
import { getVerificationTokenByToken } from '@/data/verification-token'
export const newVerification = async (token: string) => {
// if no token, display message
const exisitingToken = await getVerificationTokenByToken(token)
try {
const existingToken = await getVerificationTokenByToken(token)
if (!exisitingToken) {
return { error: 'Token does not exisit!' }
}
// if token has expired, display message
const hasExpired = new Date(exisitingToken.expires) < new Date()
if (hasExpired) {
return { error: 'Token has expired!' }
}
// if user does not exist, display message
const existingUser = await getUserByEmail(exisitingToken.email)
if (!existingUser) {
return { error: 'User does not exisit!' }
}
// update email value when they verify
await db.user.update({
where: { id: existingUser.id },
data: {
emailVerified: new Date(),
email: exisitingToken.email
if (!existingToken) {
return { error: 'Verification link is invalid!' }
}
})
// delete token
await db.verificationToken.delete({
where: { id: exisitingToken.id }
})
return { success: 'Email verified! Login to continue' }
const hasExpired = new Date(existingToken.expires) < new Date()
if (hasExpired) {
return { error: 'Verification link has expired! Please request a new one.' }
}
const existingUser = await getUserByEmail(existingToken.email)
if (!existingUser) {
return { error: 'Email not found! Please sign up first.' }
}
// If already verified, just return success
if (existingUser.emailVerified) {
return { success: 'Email already verified! Please login.' }
}
// Update user verification status
await db.user.update({
where: { id: existingUser.id },
data: {
emailVerified: new Date(),
email: existingToken.email
}
})
// Delete the verification token
await db.verificationToken.delete({
where: { id: existingToken.id }
})
return { success: 'Email verified successfully! You can now login.' }
} catch (error) {
return { error: 'Something went wrong! Please try again.' }
}
}