From 85bda9c63dfc9ebec7cb82054e1a30975f123c79 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Fri, 30 May 2025 19:02:55 +0200 Subject: [PATCH] fix: handle when userinfo has no email address (#60557) --- api/src/plugins/auth0.test.ts | 18 ++++++++++++++---- api/src/plugins/auth0.ts | 11 ++++++++--- client/i18n/locales/english/translations.json | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/api/src/plugins/auth0.test.ts b/api/src/plugins/auth0.test.ts index a3479c139ac..23a1466218c 100644 --- a/api/src/plugins/auth0.test.ts +++ b/api/src/plugins/auth0.test.ts @@ -208,13 +208,18 @@ describe('auth0 plugin', () => { token: 'any token' }); userinfoSpy.mockResolvedValueOnce(Promise.reject(Error('any error'))); + const returnTo = 'https://www.freecodecamp.org/espanol/learn'; const res = await fastify.inject({ method: 'GET', - url: '/auth/auth0/callback?state=valid' + url: '/auth/auth0/callback?state=valid', + cookies: { 'login-returnto': sign(returnTo) } }); - expect(res.headers.location).toMatch('/signin'); + expect(res.headers.location).toMatch( + returnTo + + `?${formatMessage({ type: 'danger', content: 'flash.generic-error' })}` + ); expect(res.statusCode).toBe(302); expect(await fastify.prisma.user.count()).toBe(0); }); @@ -224,13 +229,18 @@ describe('auth0 plugin', () => { token: 'any token' }); userinfoSpy.mockResolvedValueOnce(Promise.resolve({})); + const returnTo = 'https://www.freecodecamp.org/espanol/learn'; const res = await fastify.inject({ method: 'GET', - url: '/auth/auth0/callback?state=valid' + url: '/auth/auth0/callback?state=valid', + cookies: { 'login-returnto': sign(returnTo) } }); - expect(res.headers.location).toMatch('/signin'); + expect(res.headers.location).toMatch( + returnTo + + `?${formatMessage({ type: 'danger', content: 'flash.no-email-in-userinfo' })}` + ); expect(res.statusCode).toBe(302); expect(await fastify.prisma.user.count()).toBe(0); }); diff --git a/api/src/plugins/auth0.ts b/api/src/plugins/auth0.ts index bbb60f69ff6..515408e92f3 100644 --- a/api/src/plugins/auth0.ts +++ b/api/src/plugins/auth0.ts @@ -144,13 +144,18 @@ export const auth0Client: FastifyPluginCallbackTypebox = fp( logger.info(`Auth0 userinfo: ${JSON.stringify(userinfo)}`); email = userinfo.email; if (typeof email !== 'string') { - const msg = `Invalid userinfo email: ${JSON.stringify(userinfo)}`; - throw Error(msg); + return reply.redirectWithMessage(returnTo, { + type: 'danger', + content: 'flash.no-email-in-userinfo' + }); } } catch (error) { logger.error(error, 'Failed to get userinfo from Auth0'); fastify.Sentry.captureException(error); - return reply.redirect('/signin'); + return reply.redirectWithMessage(returnTo, { + type: 'danger', + content: 'flash.generic-error' + }); } const { id, acceptedPrivacyTerms } = await findOrCreateUser( diff --git a/client/i18n/locales/english/translations.json b/client/i18n/locales/english/translations.json index a2c54f1bacb..4b6e3531d33 100644 --- a/client/i18n/locales/english/translations.json +++ b/client/i18n/locales/english/translations.json @@ -852,6 +852,7 @@ "edit-my-profile": "Edit my profile" }, "flash": { + "no-email-in-userinfo": "We could not retrieve an email from your chosen provider. Please try another provider or use the 'Continue with Email' option.", "honest-first": "To claim a certification, you must first agree to our academic honesty policy", "really-weird": "Something really weird happened, if it happens again, please consider raising an issue on https://github.com/freeCodeCamp/freeCodeCamp/issues/new", "generic-error": "Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.",