With frontendApiProxy enabled, the first API request after sign-in returns signed-out (client-uat-but-no-session-token)

未關閉
#10,790 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@ShivamJuyal24 已經在處理了。

開始於 2026年11月14日。

評估

難度
3/5
預估耗時
半天
新手友好度
62/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍

研究方向

Start in the backend package's AuthenticateContext: read usesSuffixedCookies() and tokenBelongsToInstance(), and compare the token issuer against originalFrontendApi and the proxy URL the issue describes. Reproduce with a non-document request carrying __client_uat and __session without a suffix, using a proxy-configured clerkMiddleware. Done when such a request resolves as signed in and the existing suffixed-cookie tests still pass.

由索引模型根據 Issue 內容生成。

描述

Title: With frontendApiProxy enabled, the first API request after sign-in returns signed-out (client-uat-but-no-session-token)

Packages
  • @clerk/nextjs 7.2.5 (@clerk/backend 3.3.0)
  • The same code is in @clerk/backend 3.27.0 (latest at the time of writing)
  • Production instance, Next.js 16 App Router, deployed on Vercel
Setup
  • clerkMiddleware(handler, { frontendApiProxy: { enabled: true, path: "/__clerk" } })
  • NEXT_PUBLIC_CLERK_PROXY_URL=https://<our-domain>/__clerk
  • The proxy is enabled in the Clerk dashboard with the same URL
  • The publishable key's Frontend API is clerk.<our-domain>
What happens
  1. Sign in with Google OAuth (via /sso-callback and AuthenticateWithRedirectCallback).
  2. The app navigates client-side to a protected page, which calls fetch("/api/projects").
  3. That request gets a 401: the middleware's auth() returns userId: null.
  4. A hard refresh fixes it: the document request goes through a handshake, and after that every request works.

It happens on every sign-in, in a fresh browser too.

Cookies on the failing request
__client_uat=1794509021
__client_uat_<suffix>=1794509017
__session=<JWT: iss="https://<our-domain>/__clerk", azp="https://<our-domain>", iat=1794509021>
(no __session_<suffix>)
Cause, from reading the source

AuthenticateContext.usesSuffixedCookies() returns true early when the plain session token does not belong to the instance:

if (session && !this.tokenBelongsToInstance(session)) {
  return true;
}

tokenBelongsToInstance() compares the token's iss, without its scheme, against this.originalFrontendApi. That value comes from the publishable key (clerk.<our-domain>). With a proxy, the issuer is <our-domain>/__clerk, so the check fails for every token this instance issues.

The context then reads the suffixed cookies. Right after sign-in, __client_uat_<suffix> is set but __session_<suffix> is not, so the request resolves to client-uat-but-no-session-token. A non-document request can't run a handshake, so it is treated as signed out.

Expected

A token whose issuer is the configured proxy URL counts as belonging to the instance, and the plain cookies are used as they are without a proxy.

Possible fix

In tokenBelongsToInstance, also accept the proxy URL (this.proxyUrl without its scheme) as an issuer for this instance.

Workaround

Turn the proxy off.

主要語言
TypeScript
星號
1.8k
分支
492
平均合併
1 天 7 小時
30 天內合併 PR
357

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

clerk/javascript 的其他 Issue

查看 clerk/javascript 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。