@@ -866,125 +866,6 @@ export class AuthService extends BaseService {
866866 }
867867 }
868868
869- /**
870- * Generate and store verification OTP for email
871- */
872- private async generateAndStoreVerificationOtp ( email : string ) : Promise < void > {
873- const otp = Math . floor ( 100000 + Math . random ( ) * 900000 ) . toString ( ) ; // 6-digit OTP
874- const expiresAt = new Date ( Date . now ( ) + 15 * 60 * 1000 ) ; // 15 minutes expiry
875-
876- // Store OTP in database (you may need to create a verification_otps table)
877- await this . database . insert ( schema . verificationOtps ) . values ( {
878- id : generateId ( ) ,
879- email : email . toLowerCase ( ) ,
880- otp : await this . passwordService . hash ( otp ) , // Hash the OTP for security
881- expiresAt,
882- createdAt : new Date ( )
883- } ) ;
884-
885- // TODO: Send email with OTP (integrate with email service)
886- logger . info ( 'Verification OTP generated' , { email, otp : otp . slice ( 0 , 2 ) + '****' } ) ;
887- }
888-
889- /**
890- * Verify email with OTP
891- */
892- async verifyEmailWithOtp ( email : string , otp : string , request : Request ) : Promise < AuthResult > {
893- try {
894- // Deployment-level admission gate (ALLOWED_EMAIL)
895- enforceAllowedEmail ( this . env , email , 'login' ) ;
896-
897- // Find valid OTP
898- const storedOtp = await this . database
899- . select ( )
900- . from ( schema . verificationOtps )
901- . where (
902- and (
903- eq ( schema . verificationOtps . email , email . toLowerCase ( ) ) ,
904- eq ( schema . verificationOtps . used , false ) ,
905- sql `${ schema . verificationOtps . expiresAt } > ${ new Date ( ) } `
906- )
907- )
908- . orderBy ( sql `${ schema . verificationOtps . createdAt } DESC` )
909- . get ( ) ;
910-
911- if ( ! storedOtp ) {
912- throw new SecurityError (
913- SecurityErrorType . INVALID_INPUT ,
914- 'Invalid or expired verification code' ,
915- 400
916- ) ;
917- }
918-
919- // Verify OTP
920- const otpValid = await this . passwordService . verify ( otp , storedOtp . otp ) ;
921- if ( ! otpValid ) {
922- throw new SecurityError (
923- SecurityErrorType . INVALID_INPUT ,
924- 'Invalid verification code' ,
925- 400
926- ) ;
927- }
928-
929- // Mark OTP as used
930- await this . database
931- . update ( schema . verificationOtps )
932- . set ( { used : true , usedAt : new Date ( ) } )
933- . where ( eq ( schema . verificationOtps . id , storedOtp . id ) ) ;
934-
935- // Find and verify the user
936- const user = await this . database
937- . select ( )
938- . from ( schema . users )
939- . where ( eq ( schema . users . email , email . toLowerCase ( ) ) )
940- . get ( ) ;
941-
942- if ( ! user ) {
943- throw new SecurityError (
944- SecurityErrorType . INVALID_INPUT ,
945- 'User not found' ,
946- 404
947- ) ;
948- }
949-
950- // Update user as verified
951- await this . database
952- . update ( schema . users )
953- . set ( { emailVerified : true , updatedAt : new Date ( ) } )
954- . where ( eq ( schema . users . id , user . id ) ) ;
955-
956- // Create session for verified user
957- const { accessToken, session } = await this . sessionService . createSession (
958- user . id ,
959- request
960- ) ;
961-
962- // Log successful verification
963- await this . logAuthAttempt ( email , 'email_verification' , true , request ) ;
964- logger . info ( 'Email verified successfully' , { email, userId : user . id } ) ;
965-
966- return {
967- user : mapUserResponse ( { ...user , emailVerified : true } ) ,
968- accessToken,
969- sessionId : session . sessionId ,
970- expiresAt : session . expiresAt ,
971- } ;
972- } catch ( error ) {
973- await this . logAuthAttempt ( email , 'email_verification' , false , request ) ;
974-
975- if ( error instanceof SecurityError ) {
976- throw error ;
977- }
978-
979- logger . error ( 'Email verification error' , error ) ;
980- throw new SecurityError (
981- SecurityErrorType . INVALID_INPUT ,
982- 'Email verification failed' ,
983- 500
984- ) ;
985- }
986- }
987-
988869 /**
989870 * Get user for authentication (for middleware)
990871 */
@@ -1120,60 +1001,4 @@ export class AuthService extends BaseService {
11201001 }
11211002 }
11221003
1123- /**
1124- * Resend verification OTP
1125- */
1126- async resendVerificationOtp ( email : string ) : Promise < void > {
1127- try {
1128- // Check if user exists and is unverified
1129- const user = await this . database
1130- . select ( )
1131- . from ( schema . users )
1132- . where ( eq ( schema . users . email , email . toLowerCase ( ) ) )
1133- . get ( ) ;
1134-
1135- if ( ! user ) {
1136- throw new SecurityError (
1137- SecurityErrorType . INVALID_INPUT ,
1138- 'No account found with this email' ,
1139- 404
1140- ) ;
1141- }
1142-
1143- if ( user . emailVerified ) {
1144- throw new SecurityError (
1145- SecurityErrorType . INVALID_INPUT ,
1146- 'Email is already verified' ,
1147- 400
1148- ) ;
1149- }
1150-
1151- // Invalidate existing OTPs
1152- await this . database
1153- . update ( schema . verificationOtps )
1154- . set ( { used : true , usedAt : new Date ( ) } )
1155- . where (
1156- and (
1157- eq ( schema . verificationOtps . email , email . toLowerCase ( ) ) ,
1158- eq ( schema . verificationOtps . used , false )
1159- )
1160- ) ;
1161-
1162- // Generate new OTP
1163- await this . generateAndStoreVerificationOtp ( email . toLowerCase ( ) ) ;
1164-
1165- logger . info ( 'Verification OTP resent' , { email } ) ;
1166- } catch ( error ) {
1167- if ( error instanceof SecurityError ) {
1168- throw error ;
1169- }
1170-
1171- logger . error ( 'Resend verification OTP error' , error ) ;
1172- throw new SecurityError (
1173- SecurityErrorType . INVALID_INPUT ,
1174- 'Failed to resend verification code' ,
1175- 500
1176- ) ;
1177- }
1178- }
11791004}
0 commit comments