Skip to content

Commit

Permalink
chore(repo): Switch from beta-v5 to beta
Browse files Browse the repository at this point in the history
* chore(repo): Switch from beta-v5 to beta

* fix(clerk-js): Port cookie-token sync fix from v4

* fix(nextjs): Do not set AuthReason on Response unless needed
  • Loading branch information
nikosdouvlis authored Feb 16, 2024
1 parent 528af39 commit 45c9200
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-poets-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Ensure the token returned from `getToken()` and the token in the session cookie remain in sync.
2 changes: 1 addition & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"mode": "pre",
"tag": "beta-v5",
"tag": "beta",
"initialVersions": {
"@clerk/backend": "0.31.3",
"@clerk/chrome-extension": "0.4.10",
Expand Down
6 changes: 5 additions & 1 deletion packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ export class Session extends BaseResource implements SessionResource {
const cachedEntry = skipCache ? undefined : SessionTokenCache.get({ tokenId }, leewayInSeconds);

if (cachedEntry) {
return cachedEntry.tokenResolver.then(res => res.getRawString());
const cachedToken = await cachedEntry.tokenResolver.then(res => res);
if (!template) {
eventBus.dispatch(events.TokenUpdate, { token: cachedToken });
}
return cachedToken.getRawString();
}
const path = template ? `${this.path()}/tokens/${template}` : `${this.path()}/tokens`;
const tokenResolver = Token.create(path);
Expand Down
5 changes: 0 additions & 5 deletions packages/nextjs/src/server/authMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ describe('authMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toEqual('https://www.clerk.com/custom-redirect');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).not.toBeCalled();
expect(afterAuthSpy).not.toBeCalled();
});
Expand Down Expand Up @@ -385,7 +384,6 @@ describe('authMiddleware(params)', () => {
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand Down Expand Up @@ -435,7 +433,6 @@ describe('Dev Browser JWT when redirecting to cross origin', function () {
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -448,7 +445,6 @@ describe('Dev Browser JWT when redirecting to cross origin', function () {
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected&__clerk_db_jwt=test_jwt',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -466,7 +462,6 @@ describe('Dev Browser JWT when redirecting to cross origin', function () {
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});
});
Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs/src/server/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const authMiddleware: AuthMiddleware = (...args: unknown[]) => {
return setHeader(NextResponse.next(), constants.Headers.AuthReason, 'skip');
} else if (beforeAuthRes && isRedirect(beforeAuthRes)) {
logger.debug('Before auth returned redirect, following redirect');
return setHeader(beforeAuthRes, constants.Headers.AuthReason, 'redirect');
return setHeader(beforeAuthRes, constants.Headers.AuthReason, 'before-auth-redirect');
}

const requestState = await clerkClient.authenticateRequest(
Expand Down Expand Up @@ -206,8 +206,7 @@ const authMiddleware: AuthMiddleware = (...args: unknown[]) => {

if (isRedirect(finalRes)) {
logger.debug('Final response is redirect, following redirect');
const res = setHeader(finalRes, constants.Headers.AuthReason, 'redirect');
return serverRedirectWithAuth(clerkRequest, res, options);
return serverRedirectWithAuth(clerkRequest, finalRes, options);
}

if (options.debug) {
Expand Down
14 changes: 0 additions & 14 deletions packages/nextjs/src/server/clerkMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ describe('clerkMiddleware(params)', () => {
expect(signInResp?.status).toEqual(307);
expect(signInResp?.headers.get(constants.Headers.Location)).toEqual('https://www.clerk.com/hello');
expect(signInResp?.headers.get('a-custom-header')).toEqual('1');
expect(signInResp?.headers.get(constants.Headers.AuthReason)).toBeTruthy();
});

describe('auth().redirectToSignIn()', () => {
Expand All @@ -228,7 +227,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('sign-in');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -246,7 +244,6 @@ describe('clerkMiddleware(params)', () => {
expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('sign-in');
expect(new URL(resp!.headers.get('location')!).searchParams.get('redirect_url')).toContain('/protected');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -266,7 +263,6 @@ describe('clerkMiddleware(params)', () => {
expect(new URL(resp!.headers.get('location')!).searchParams.get('redirect_url')).toEqual(
'https://www.clerk.com/hello',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -284,7 +280,6 @@ describe('clerkMiddleware(params)', () => {
expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('sign-in');
expect(new URL(resp!.headers.get('location')!).searchParams.get('redirect_url')).toBeNull();
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});
});
Expand All @@ -309,7 +304,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('sign-in');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand Down Expand Up @@ -398,7 +392,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toEqual('https://www.clerk.com/hello');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(resp?.headers.get(constants.Headers.ClerkRedirectTo)).toEqual('true');
expect(clerkClient.authenticateRequest).toBeCalled();
});
Expand Down Expand Up @@ -428,7 +421,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toEqual('https://www.clerk.com/discover');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(resp?.headers.get(constants.Headers.ClerkRedirectTo)).toEqual('true');
expect(clerkClient.authenticateRequest).toBeCalled();
});
Expand All @@ -454,7 +446,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('sign-in');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -480,7 +471,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('https://www.clerk.com/unauthenticatedUrl');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(resp?.headers.get(constants.Headers.ClerkRedirectTo)).toEqual('true');
expect(clerkClient.authenticateRequest).toBeCalled();
});
Expand Down Expand Up @@ -510,7 +500,6 @@ describe('clerkMiddleware(params)', () => {

expect(resp?.status).toEqual(307);
expect(resp?.headers.get('location')).toContain('https://www.clerk.com/unauthorizedUrl');
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(resp?.headers.get(constants.Headers.ClerkRedirectTo)).toEqual('true');
expect(clerkClient.authenticateRequest).toBeCalled();
});
Expand Down Expand Up @@ -539,7 +528,6 @@ describe('Dev Browser JWT when redirecting to cross origin for page requests', f
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -563,7 +551,6 @@ describe('Dev Browser JWT when redirecting to cross origin for page requests', f
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected&__clerk_db_jwt=test_jwt',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});

Expand All @@ -589,7 +576,6 @@ describe('Dev Browser JWT when redirecting to cross origin for page requests', f
expect(resp?.headers.get('location')).toEqual(
'https://accounts.included.katydid-92.lcl.dev/sign-in?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fprotected',
);
expect(resp?.headers.get('x-clerk-auth-reason')).toEqual('redirect');
expect(clerkClient.authenticateRequest).toBeCalled();
});
});
3 changes: 1 addition & 2 deletions packages/nextjs/src/server/clerkMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ export const clerkMiddleware: ClerkMiddleware = (...args: unknown[]): any => {
}

if (isRedirect(handlerResult)) {
const res = setHeader(handlerResult, constants.Headers.AuthReason, 'redirect');
return serverRedirectWithAuth(clerkRequest, res, options);
return serverRedirectWithAuth(clerkRequest, handlerResult, options);
}

if (options.debug) {
Expand Down

0 comments on commit 45c9200

Please sign in to comment.