24 lines
851 B
TypeScript
24 lines
851 B
TypeScript
'use server'
|
|
|
|
import { deleteVerificationToken, getVerificationTokenByToken } from '@/data/verification-token'
|
|
import { getUserByEmail, updateUserEmailVerified } 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!' }
|
|
|
|
await updateUserEmailVerified(existingUser.id, existingToken.email)
|
|
|
|
await deleteVerificationToken(existingToken.id)
|
|
|
|
return { success: 'User verified!' }
|
|
} |