mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-17 07:00:45 -04:00
refactor(api): sync dev and auth0 plugins (#57136)
This commit is contained in:
committed by
GitHub
parent
c69e9c6fff
commit
2f4e6ae8f5
48
api/src/plugins/auth-dev.ts
Normal file
48
api/src/plugins/auth-dev.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebox';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import {
|
||||
getRedirectParams,
|
||||
getPrefixedLandingPath,
|
||||
haveSamePath
|
||||
} from '../utils/redirection';
|
||||
import { findOrCreateUser } from '../routes/helpers/auth-helpers';
|
||||
import { createAccessToken } from '../utils/tokens';
|
||||
|
||||
const trimTrailingSlash = (str: string) =>
|
||||
str.endsWith('/') ? str.slice(0, -1) : str;
|
||||
|
||||
async function handleRedirects(req: FastifyRequest, reply: FastifyReply) {
|
||||
const params = getRedirectParams(req);
|
||||
const { origin, pathPrefix } = params;
|
||||
const returnTo = trimTrailingSlash(params.returnTo);
|
||||
const landingUrl = getPrefixedLandingPath(origin, pathPrefix);
|
||||
|
||||
return await reply.redirect(
|
||||
haveSamePath(landingUrl, returnTo) ? `${returnTo}/learn` : returnTo
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fastify plugin for dev authentication.
|
||||
*
|
||||
* @param fastify - The Fastify instance.
|
||||
* @param _options - The plugin options.
|
||||
* @param done - The callback function.
|
||||
*/
|
||||
export const devAuth: FastifyPluginCallbackTypebox = (
|
||||
fastify,
|
||||
_options,
|
||||
done
|
||||
) => {
|
||||
fastify.get('/signin', async (req, reply) => {
|
||||
const email = 'foo@bar.com';
|
||||
|
||||
const { id } = await findOrCreateUser(fastify, email);
|
||||
|
||||
reply.setAccessTokenCookie(createAccessToken(id));
|
||||
|
||||
await handleRedirects(req, reply);
|
||||
});
|
||||
|
||||
done();
|
||||
};
|
||||
Reference in New Issue
Block a user