mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat/ab test landing google auth (#62538)
This commit is contained in:
@@ -42,6 +42,37 @@ describe('auth0 plugin', () => {
|
||||
await fastify.register(prismaPlugin);
|
||||
});
|
||||
|
||||
describe('GET /signin/google', () => {
|
||||
test('should redirect directly to Google via Auth0 with connection param', async () => {
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin/google'
|
||||
});
|
||||
const redirectUrl = new URL(res.headers.location!);
|
||||
expect(redirectUrl.host).toMatch(AUTH0_DOMAIN);
|
||||
expect(redirectUrl.pathname).toBe('/authorize');
|
||||
expect(redirectUrl.searchParams.get('connection')).toBe('google-oauth2');
|
||||
expect(res.statusCode).toBe(302);
|
||||
});
|
||||
|
||||
test('sets a login-returnto cookie', async () => {
|
||||
const returnTo = 'http://localhost:3000/learn';
|
||||
const res = await fastify.inject({
|
||||
method: 'GET',
|
||||
url: '/signin/google',
|
||||
headers: { referer: returnTo }
|
||||
});
|
||||
const cookie = res.cookies.find(c => c.name === 'login-returnto');
|
||||
expect(unsign(cookie!.value).value).toBe(returnTo);
|
||||
expect(cookie).toMatchObject({
|
||||
domain: COOKIE_DOMAIN,
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: 'Lax'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await fastify.prisma.$runCommandRaw({ dropDatabase: 1 });
|
||||
await fastify.close();
|
||||
|
||||
@@ -82,6 +82,25 @@ export const auth0Client: FastifyPluginCallbackTypebox = fp(
|
||||
);
|
||||
void reply.redirect(redirectUrl);
|
||||
});
|
||||
|
||||
fastify.get('/signin/google', async function (request, reply) {
|
||||
const returnTo = request.headers.referer ?? `${HOME_LOCATION}/learn`;
|
||||
void reply.setCookie('login-returnto', returnTo, {
|
||||
domain: COOKIE_DOMAIN,
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
signed: true,
|
||||
sameSite: 'lax'
|
||||
});
|
||||
|
||||
const authorizationEndpoint =
|
||||
await this.auth0OAuth.generateAuthorizationUri(request, reply);
|
||||
|
||||
const url = new URL(authorizationEndpoint);
|
||||
url.searchParams.set('connection', 'google-oauth2');
|
||||
|
||||
void reply.redirect(url.toString());
|
||||
});
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user