Skip to content

Commit 5f0befe

Browse files
authored
fix(nextjs): Set keyless cookie on Safari (#5696)
1 parent 25c3502 commit 5f0befe

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

.changeset/thick-beers-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/nextjs': patch
3+
---
4+
5+
Resolved an issue with Keyless on Safari where users appeared to be signed out immediately after a successful sign-in.

packages/nextjs/src/app-router/keyless-actions.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import { detectClerkMiddleware } from '../server/headers-utils';
88
import { getKeylessCookieName, getKeylessCookieValue } from '../server/keyless';
99
import { canUseKeyless } from '../utils/feature-flags';
1010

11+
type SetCookieOptions = Parameters<Awaited<ReturnType<typeof cookies>>['set']>[2];
12+
13+
const keylessCookieConfig = {
14+
secure: false,
15+
httpOnly: false,
16+
sameSite: 'lax',
17+
} satisfies SetCookieOptions;
18+
1119
export async function syncKeylessConfigAction(args: AccountlessApplication & { returnUrl: string }): Promise<void> {
1220
const { claimUrl, publishableKey, secretKey, returnUrl } = args;
1321
const cookieStore = await cookies();
@@ -22,10 +30,11 @@ export async function syncKeylessConfigAction(args: AccountlessApplication & { r
2230
}
2331

2432
// Set the new keys in the cookie.
25-
cookieStore.set(await getKeylessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), {
26-
secure: true,
27-
httpOnly: true,
28-
});
33+
cookieStore.set(
34+
await getKeylessCookieName(),
35+
JSON.stringify({ claimUrl, publishableKey, secretKey }),
36+
keylessCookieConfig,
37+
);
2938

3039
// We cannot import `NextRequest` due to a bundling issue with server actions in Next.js 13.
3140
// @ts-expect-error Request will work as well
@@ -63,11 +72,11 @@ export async function createOrReadKeylessAction(): Promise<null | Omit<Accountle
6372
});
6473

6574
const { claimUrl, publishableKey, secretKey, apiKeysUrl } = result;
66-
67-
void (await cookies()).set(await getKeylessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), {
68-
secure: false,
69-
httpOnly: false,
70-
});
75+
void (await cookies()).set(
76+
await getKeylessCookieName(),
77+
JSON.stringify({ claimUrl, publishableKey, secretKey }),
78+
keylessCookieConfig,
79+
);
7180

7281
return {
7382
claimUrl,

0 commit comments

Comments
 (0)