Skip to content
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/wet-donuts-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/nextjs': patch
---

Append `return_url` when logging the claimUrl for on Keyless.
23 changes: 22 additions & 1 deletion packages/nextjs/src/app-router/server/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ const getNonceFromCSPHeader = React.cache(async function getNonceFromCSPHeader()
return getScriptNonceFromHeader((await headers()).get('Content-Security-Policy') || '') || '';
});

/** Discards errors thrown by attempted code */
const onlyTry = (cb: () => unknown) => {
try {
cb();
} catch (e) {
// ignore
}
};

export async function ClerkProvider(
props: Without<NextClerkProviderProps, '__unstable_invokeMiddlewareOnAuthStateChange'>,
) {
Expand Down Expand Up @@ -111,12 +120,24 @@ export async function ClerkProvider(
} else {
const KeylessCookieSync = await import('../client/keyless-cookie-sync.js').then(mod => mod.KeylessCookieSync);

const headerStore = await headers();
/**
* Allow developer to return to local application after claiming
*/
const host = headerStore.get('x-forwarded-host');
const proto = headerStore.get('x-forwarded-proto');

const claimUrl = new URL(newOrReadKeys.claimUrl);
if (host && proto) {
onlyTry(() => claimUrl.searchParams.set('return_url', new URL(`${proto}://${host}`).href));
}

/**
* Notify developers.
*/
keylessLogger?.log({
cacheKey: newOrReadKeys.publishableKey,
msg: createKeylessModeMessage(newOrReadKeys),
msg: createKeylessModeMessage({ ...newOrReadKeys, claimUrl: claimUrl.href }),
});

output = <KeylessCookieSync {...newOrReadKeys}>{clientProvider}</KeylessCookieSync>;
Expand Down
Loading