diff --git a/api/server/services/AuthService.js b/api/server/services/AuthService.js index 8f6c281ee1..a4a59bb8b6 100644 --- a/api/server/services/AuthService.js +++ b/api/server/services/AuthService.js @@ -218,16 +218,17 @@ const createTokenHash = () => { */ const sendVerificationEmail = async (user) => { const [verifyToken, hash] = createTokenHash(); + const email = user.email.toLowerCase(); const verificationLink = `${ domains.client - }/verify?token=${verifyToken}&email=${encodeURIComponent(user.email)}`; + }/verify?token=${verifyToken}&email=${encodeURIComponent(email)}`; await sendEmail({ - email: user.email, + email, subject: 'Verify your email', payload: { appName: process.env.APP_TITLE || 'LibreChat', - name: user.name || user.username || user.email, + name: user.name || user.username || email, verificationLink: verificationLink, year: new Date().getFullYear(), }, @@ -236,14 +237,14 @@ const sendVerificationEmail = async (user) => { await createToken({ userId: user._id, - email: user.email, + email, type: AuthTokenTypes.EMAIL_VERIFICATION, token: hash, createdAt: Date.now(), expiresIn: 900, }); - logger.info(`[sendVerificationEmail] Verification link issued. [Email: ${user.email}]`); + logger.info(`[sendVerificationEmail] Verification link issued. [Email: ${email}]`); }; /** diff --git a/api/server/services/AuthService.spec.js b/api/server/services/AuthService.spec.js index 03579e7278..ce4dd517ff 100644 --- a/api/server/services/AuthService.spec.js +++ b/api/server/services/AuthService.spec.js @@ -471,6 +471,31 @@ describe('registerUser', () => { }), ); }); + + it('normalizes mixed-case emails when issuing the verification token and link', async () => { + checkEmailConfig.mockReturnValue(true); + + const result = await registerUser({ + ...registrationPayload, + email: 'Mixed.Case@Example.com', + }); + + expect(result.status).toBe(200); + expect(createToken).toHaveBeenCalledWith( + expect.objectContaining({ + email: 'mixed.case@example.com', + type: 'email_verification', + }), + ); + expect(sendEmail).toHaveBeenCalledWith( + expect.objectContaining({ + email: 'mixed.case@example.com', + payload: expect.objectContaining({ + verificationLink: expect.stringContaining(encodeURIComponent('mixed.case@example.com')), + }), + }), + ); + }); }); describe('verifyEmail public response handling', () => {