11import bcrypt from 'bcryptjs' ;
22import { PrismaClient } from '@prisma/client' ;
33import { RegisterRequest , LoginRequest , AuthResponse } from '../types/auth' ;
4- import { generateTokens , generatePasswordResetToken } from '../utils/jwt' ;
4+ import { generateTokens , generatePasswordResetToken , generateEmailVerificationToken , verifyEmailVerificationToken } from '../utils/jwt' ;
55import { sendVerificationEmail , sendPasswordResetEmail } from '../utils/email' ;
66
77const prisma = new PrismaClient ( ) ;
@@ -72,7 +72,7 @@ export class AuthService {
7272
7373 // Send verification email (don't await to avoid blocking)
7474 if ( process . env . EMAIL_HOST && process . env . EMAIL_USER ) {
75- const verificationToken = generatePasswordResetToken ( user . id ) ;
75+ const verificationToken = generateEmailVerificationToken ( user . id ) ;
7676 sendVerificationEmail ( user . email , verificationToken )
7777 . then ( async ( ) => {
7878 // Mark welcome email as sent
@@ -228,8 +228,7 @@ export class AuthService {
228228
229229 async verifyEmail ( token : string ) : Promise < void > {
230230 try {
231- const { verifyPasswordResetToken } = await import ( '../utils/jwt' ) ;
232- const { userId } = verifyPasswordResetToken ( token ) ;
231+ const { userId } = verifyEmailVerificationToken ( token ) ;
233232
234233 await prisma . user . update ( {
235234 where : { id : userId } ,
@@ -240,6 +239,36 @@ export class AuthService {
240239 }
241240 }
242241
242+ async resendVerificationEmail ( userId : string ) : Promise < void > {
243+ const user = await prisma . user . findUnique ( {
244+ where : { id : userId } ,
245+ select : { email : true , emailVerified : true }
246+ } ) ;
247+
248+ if ( ! user ) {
249+ throw new Error ( 'User not found' ) ;
250+ }
251+
252+ if ( user . emailVerified ) {
253+ throw new Error ( 'Email is already verified' ) ;
254+ }
255+
256+ // Generate new verification token
257+ const verificationToken = generateEmailVerificationToken ( userId ) ;
258+
259+ // Send verification email
260+ await sendVerificationEmail ( user . email , verificationToken ) ;
261+
262+ // Update the welcomeEmailSentAt timestamp
263+ await prisma . user . update ( {
264+ where : { id : userId } ,
265+ data : {
266+ welcomeEmailSentAt : new Date ( ) ,
267+ welcomeEmailSent : true
268+ }
269+ } ) ;
270+ }
271+
243272 async getUserById ( userId : string ) {
244273 return await prisma . user . findUnique ( {
245274 where : { id : userId } ,
0 commit comments