feat/ab test landing google auth (#62538)

This commit is contained in:
Ahmad Abdolsaheb
2025-10-29 16:55:08 +03:00
committed by GitHub
parent 2bd3ec1db8
commit 03c775ac2d
14 changed files with 464 additions and 240 deletions
+31
View File
@@ -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();
+19
View File
@@ -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();
});