fix(auth): fallback to env token in useAuth and mutator for SSR

Local auth was using getLocalAuthToken() which only checks in-memory
and sessionStorage — both unavailable during SSR. The env token
(NEXT_PUBLIC_LOCAL_AUTH_TOKEN) was defined but never called in the
auth flow. Now useAuth, hasLocalAuthToken, and customFetch all fall
back to getEnvToken() so SSR correctly identifies the local user as
signed in and admin.
This commit is contained in:
null 2026-05-20 03:29:15 -05:00
parent 9fada7dd5c
commit d85912c4c9
2 changed files with 5 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth"; import { getEnvToken, getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
import { getApiBaseUrl } from "@/lib/api-base"; import { getApiBaseUrl } from "@/lib/api-base";
type ClerkSession = { type ClerkSession = {
@ -48,7 +48,7 @@ export const customFetch = async <T>(
headers.set("Content-Type", "application/json"); headers.set("Content-Type", "application/json");
} }
if (isLocalAuthMode() && !headers.has("Authorization")) { if (isLocalAuthMode() && !headers.has("Authorization")) {
const token = getLocalAuthToken(); const token = getLocalAuthToken() ?? getEnvToken();
if (token) { if (token) {
headers.set("Authorization", `Bearer ${token}`); headers.set("Authorization", `Bearer ${token}`);
} }

View File

@ -16,10 +16,10 @@ import {
} from "@clerk/nextjs"; } from "@clerk/nextjs";
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey"; import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
import { getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth"; import { getEnvToken, getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
function hasLocalAuthToken(): boolean { function hasLocalAuthToken(): boolean {
return Boolean(getLocalAuthToken()); return Boolean(getLocalAuthToken() ?? getEnvToken());
} }
export function isClerkEnabled(): boolean { export function isClerkEnabled(): boolean {
@ -76,7 +76,7 @@ export function useUser() {
export function useAuth() { export function useAuth() {
if (isLocalAuthMode()) { if (isLocalAuthMode()) {
const token = getLocalAuthToken(); const token = getLocalAuthToken() ?? getEnvToken();
return { return {
isLoaded: true, isLoaded: true,
isSignedIn: Boolean(token), isSignedIn: Boolean(token),