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