Skip to content

Commit f206ba2

Browse files
SembaukeShaunSHamiltonnaomi-lgbt
authored
feat(api): add logging to Auth0 endpoint (freeCodeCamp#59160)
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
1 parent 3104c9e commit f206ba2

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Diff for: api/src/plugins/auth0.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,43 @@ export const auth0Client: FastifyPluginCallbackTypebox = fp(
7878
});
7979

8080
// TODO: use a schema to validate the query params.
81-
fastify.get('/auth/auth0/callback', async function (request, reply) {
82-
const { error, error_description } = request.query as Record<
83-
string,
84-
string
85-
>;
81+
fastify.get('/auth/auth0/callback', async function (req, reply) {
82+
const logger = fastify.log.child({ req });
83+
84+
const { error, error_description } = req.query as Record<string, string>;
8685
if (error === 'access_denied') {
8786
const blockedByLaw =
8887
error_description === 'Access denied from your location';
89-
9088
if (blockedByLaw) {
89+
logger.info('Access denied due to user location');
9190
return reply.redirect(`${HOME_LOCATION}/blocked`);
9291
} else {
92+
logger.error('Authentication failed for user:' + error_description);
93+
9394
return reply.redirectWithMessage(`${HOME_LOCATION}/learn`, {
9495
type: 'info',
9596
content: error_description ?? 'Authentication failed'
9697
});
9798
}
9899
}
99100

100-
const { returnTo, pathPrefix, origin } = getLoginRedirectParams(request);
101+
const { returnTo, pathPrefix, origin } = getLoginRedirectParams(req);
101102
const redirectBase = getPrefixedLandingPath(origin, pathPrefix);
102103

103104
let token;
104105
try {
105106
token = (
106-
await this.auth0OAuth.getAccessTokenFromAuthorizationCodeFlow(request)
107+
await this.auth0OAuth.getAccessTokenFromAuthorizationCodeFlow(req)
107108
).token;
108109
} catch (error) {
109110
// This is the plugin's error message. If it changes, we will either
110111
// have to update the test or write custom state create/verify
111112
// functions.
112113
if (error instanceof Error && error.message === 'Invalid state') {
113-
fastify.log.error('Auth failed: invalid state');
114+
logger.error('Auth failed: invalid state');
114115
} else {
115-
fastify.log.error('Auth failed:');
116-
fastify.log.error(error);
116+
logger.error('Auth failed:');
117+
logger.error(error);
117118
fastify.Sentry.captureException(error);
118119
}
119120
// It's important _not_ to redirect to /signin here, as that could
@@ -132,7 +133,7 @@ export const auth0Client: FastifyPluginCallbackTypebox = fp(
132133
email = userinfo.email;
133134
if (typeof email !== 'string') throw Error('Invalid userinfo response');
134135
} catch (error) {
135-
fastify.log.error('Auth failed', error);
136+
logger.error({ error }, 'Auth failed');
136137
fastify.Sentry.captureException(error);
137138
return reply.redirect('/signin');
138139
}

0 commit comments

Comments
 (0)