mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat: copy and generate exam token (#62623)
Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@@ -536,6 +536,14 @@ export const userRoutes: FastifyPluginCallbackTypebox = (
|
||||
examEnvironmentTokenHandler
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
'/user/exam-environment/token',
|
||||
{
|
||||
schema: schemas.getUserExamEnvironmentToken
|
||||
},
|
||||
getExamEnvironmentToken
|
||||
);
|
||||
|
||||
fastify.get(
|
||||
'/user/exam-environment/exam/attempts',
|
||||
{
|
||||
@@ -810,3 +818,41 @@ export const userGetRoutes: FastifyPluginCallbackTypebox = (
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
async function getExamEnvironmentToken(
|
||||
this: FastifyInstance,
|
||||
req: UpdateReqType<typeof schemas.getUserExamEnvironmentToken>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
const logger = this.log.child({ req, res: reply });
|
||||
logger.info(`User ${req.user?.id} requested their exam environment token`);
|
||||
const userId = req.user?.id;
|
||||
if (!userId) {
|
||||
throw new Error('Unreachable. User should be authenticated.');
|
||||
}
|
||||
|
||||
const token = await this.prisma.examEnvironmentAuthorizationToken.findUnique({
|
||||
where: {
|
||||
userId,
|
||||
expireAt: {
|
||||
gt: new Date()
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!token) {
|
||||
void reply.code(404);
|
||||
return reply.send(
|
||||
ERRORS.FCC_ERR_EXAM_ENVIRONMENT('No valid token found for user.')
|
||||
);
|
||||
}
|
||||
|
||||
const examEnvironmentAuthorizationToken = jwt.sign(
|
||||
{ examEnvironmentAuthorizationToken: token.id },
|
||||
JWT_SECRET
|
||||
);
|
||||
|
||||
return reply.send({
|
||||
examEnvironmentAuthorizationToken
|
||||
});
|
||||
}
|
||||
|
||||
+4
-1
@@ -44,5 +44,8 @@ export { postMsUsername } from './schemas/user/post-ms-username.js';
|
||||
export { reportUser } from './schemas/user/report-user.js';
|
||||
export { resetMyProgress } from './schemas/user/reset-my-progress.js';
|
||||
export { submitSurvey } from './schemas/user/submit-survey.js';
|
||||
export { userExamEnvironmentToken } from './schemas/user/exam-environment-token.js';
|
||||
export {
|
||||
userExamEnvironmentToken,
|
||||
getUserExamEnvironmentToken
|
||||
} from './schemas/user/exam-environment-token.js';
|
||||
export { sentryPostEvent } from './schemas/sentry/event.js';
|
||||
|
||||
@@ -10,3 +10,12 @@ export const userExamEnvironmentToken = {
|
||||
// default: STANDARD_ERROR
|
||||
}
|
||||
};
|
||||
|
||||
export const getUserExamEnvironmentToken = {
|
||||
response: {
|
||||
200: Type.Object({
|
||||
examEnvironmentAuthorizationToken: Type.String()
|
||||
}),
|
||||
404: STANDARD_ERROR
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user