fix: handle when userinfo has no email address (#60557)

This commit is contained in:
Oliver Eyton-Williams
2025-05-30 19:02:55 +02:00
committed by GitHub
parent 9c87590106
commit 85bda9c63d
3 changed files with 23 additions and 7 deletions
+14 -4
View File
@@ -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);
});
+8 -3
View File
@@ -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(
@@ -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.",