Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(express): Set default enableHandshake option value to true #4248

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-chicken-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/express": minor
---

Enable handshake flow by default
24 changes: 12 additions & 12 deletions packages/express/src/__tests__/clerkMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ describe('clerkMiddleware', () => {
});

it('throws error if secretKey is not passed as parameter', async () => {
const response = await runMiddleware(clerkMiddleware({ enableHandshake: true })).expect(500);
const response = await runMiddleware(clerkMiddleware()).expect(500);

assertNoDebugHeaders(response);
});

it('works if secretKey is passed as parameter', async () => {
const options = { secretKey: 'sk_test_....', enableHandshake: true };
const options = { secretKey: 'sk_test_....' };

const response = await runMiddleware(clerkMiddleware(options), { Cookie: '__clerk_db_jwt=deadbeef;' }).expect(
200,
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('clerkMiddleware', () => {
});

it('works if publishableKey is passed as parameter', async () => {
const options = { publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k', enableHandshake: true };
const options = { publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k' };

const response = await runMiddleware(clerkMiddleware(options), { Cookie: '__clerk_db_jwt=deadbeef;' }).expect(
200,
Expand All @@ -72,7 +72,7 @@ describe('clerkMiddleware', () => {
});

it('supports usage with parameters: app.use(clerkMiddleware(options))', async () => {
const options = { publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k', enableHandshake: true };
const options = { publishableKey: 'pk_test_Y2xlcmsuZXhhbXBsZS5jb20k' };

const response = await runMiddleware(clerkMiddleware(options), { Cookie: '__clerk_db_jwt=deadbeef;' }).expect(
200,
Expand All @@ -95,23 +95,23 @@ describe('clerkMiddleware', () => {
expect(response.header).not.toHaveProperty('x-clerk-auth-custom', 'custom-value');
});

it('disables handshake flow by default', async () => {
it('handshake flow supported by default', async () => {
const response = await runMiddleware(clerkMiddleware(), {
Cookie: '__client_uat=1711618859;',
'Sec-Fetch-Dest': 'document',
}).expect(200);
}).expect(307);

assertNoDebugHeaders(response);
expect(response.header).toHaveProperty('x-clerk-auth-status', 'handshake');
expect(response.header).toHaveProperty('location', expect.stringContaining('/v1/client/handshake?redirect_url='));
});

it('supports handshake flow', async () => {
const response = await runMiddleware(clerkMiddleware({ enableHandshake: true }), {
it('can disable handshake flow', async () => {
const response = await runMiddleware(clerkMiddleware({ enableHandshake: false }), {
Cookie: '__client_uat=1711618859;',
'Sec-Fetch-Dest': 'document',
}).expect(307);
}).expect(200);

expect(response.header).toHaveProperty('x-clerk-auth-status', 'handshake');
expect(response.header).toHaveProperty('location', expect.stringContaining('/v1/client/handshake?redirect_url='));
assertNoDebugHeaders(response);
});

it('calls next with an error when request URL is invalid', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/express/src/__tests__/requireAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RequestHandler } from 'express';

import { clerkMiddleware } from '../clerkMiddleware';
import { requireAuth } from '../requireAuth';
import type { ExpressRequestWithAuth } from '../types';
import { mockRequestWithAuth, runMiddleware } from './helpers';

let mockAuthenticateAndDecorateRequest: jest.Mock;
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('requireAuth', () => {

mockAuthenticateAndDecorateRequest.mockImplementation((): RequestHandler => {
return (req, _res, next) => {
if ((req as any).auth) {
if ((req as ExpressRequestWithAuth).auth) {
return next();
}
const requestState = mockAuthenticateRequest({ request: req });
Expand Down
2 changes: 1 addition & 1 deletion packages/express/src/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const absoluteProxyUrl = (relativeOrAbsoluteUrl: string, baseUrl: string): strin

export const authenticateAndDecorateRequest = (options: ClerkMiddlewareOptions = {}) => {
const clerkClient = options.clerkClient || defaultClerkClient;
const enableHandshake = options.enableHandshake || false;
const enableHandshake = options.enableHandshake ?? true;

// eslint-disable-next-line @typescript-eslint/no-misused-promises
const middleware: RequestHandler = async (request, response, next) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/express/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type ClerkMiddlewareOptions = AuthenticateRequestOptions & {
*
* This is useful for server-rendered fullstack applications to handle
* expired JWTs securely and maintain session continuity.
*
* @default true
*/
enableHandshake?: boolean;
};
Expand Down
Loading