Skip to content

Commit 85bda9c

Browse files
fix: handle when userinfo has no email address (freeCodeCamp#60557)
1 parent 9c87590 commit 85bda9c

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

api/src/plugins/auth0.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,18 @@ describe('auth0 plugin', () => {
208208
token: 'any token'
209209
});
210210
userinfoSpy.mockResolvedValueOnce(Promise.reject(Error('any error')));
211+
const returnTo = 'https://www.freecodecamp.org/espanol/learn';
211212

212213
const res = await fastify.inject({
213214
method: 'GET',
214-
url: '/auth/auth0/callback?state=valid'
215+
url: '/auth/auth0/callback?state=valid',
216+
cookies: { 'login-returnto': sign(returnTo) }
215217
});
216218

217-
expect(res.headers.location).toMatch('/signin');
219+
expect(res.headers.location).toMatch(
220+
returnTo +
221+
`?${formatMessage({ type: 'danger', content: 'flash.generic-error' })}`
222+
);
218223
expect(res.statusCode).toBe(302);
219224
expect(await fastify.prisma.user.count()).toBe(0);
220225
});
@@ -224,13 +229,18 @@ describe('auth0 plugin', () => {
224229
token: 'any token'
225230
});
226231
userinfoSpy.mockResolvedValueOnce(Promise.resolve({}));
232+
const returnTo = 'https://www.freecodecamp.org/espanol/learn';
227233

228234
const res = await fastify.inject({
229235
method: 'GET',
230-
url: '/auth/auth0/callback?state=valid'
236+
url: '/auth/auth0/callback?state=valid',
237+
cookies: { 'login-returnto': sign(returnTo) }
231238
});
232239

233-
expect(res.headers.location).toMatch('/signin');
240+
expect(res.headers.location).toMatch(
241+
returnTo +
242+
`?${formatMessage({ type: 'danger', content: 'flash.no-email-in-userinfo' })}`
243+
);
234244
expect(res.statusCode).toBe(302);
235245
expect(await fastify.prisma.user.count()).toBe(0);
236246
});

api/src/plugins/auth0.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,18 @@ export const auth0Client: FastifyPluginCallbackTypebox = fp(
144144
logger.info(`Auth0 userinfo: ${JSON.stringify(userinfo)}`);
145145
email = userinfo.email;
146146
if (typeof email !== 'string') {
147-
const msg = `Invalid userinfo email: ${JSON.stringify(userinfo)}`;
148-
throw Error(msg);
147+
return reply.redirectWithMessage(returnTo, {
148+
type: 'danger',
149+
content: 'flash.no-email-in-userinfo'
150+
});
149151
}
150152
} catch (error) {
151153
logger.error(error, 'Failed to get userinfo from Auth0');
152154
fastify.Sentry.captureException(error);
153-
return reply.redirect('/signin');
155+
return reply.redirectWithMessage(returnTo, {
156+
type: 'danger',
157+
content: 'flash.generic-error'
158+
});
154159
}
155160

156161
const { id, acceptedPrivacyTerms } = await findOrCreateUser(

client/i18n/locales/english/translations.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@
852852
"edit-my-profile": "Edit my profile"
853853
},
854854
"flash": {
855+
"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.",
855856
"honest-first": "To claim a certification, you must first agree to our academic honesty policy",
856857
"really-weird": "Something really weird happened, if it happens again, please consider raising an issue on https://github.com/freeCodeCamp/freeCodeCamp/issues/new",
857858
"generic-error": "Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.",

0 commit comments

Comments
 (0)