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:
parent
9fada7dd5c
commit
d85912c4c9
|
|
@ -1,4 +1,4 @@
|
|||
import { getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
|
||||
import { getEnvToken, getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
|
||||
import { getApiBaseUrl } from "@/lib/api-base";
|
||||
|
||||
type ClerkSession = {
|
||||
|
|
@ -48,7 +48,7 @@ export const customFetch = async <T>(
|
|||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
if (isLocalAuthMode() && !headers.has("Authorization")) {
|
||||
const token = getLocalAuthToken();
|
||||
const token = getLocalAuthToken() ?? getEnvToken();
|
||||
if (token) {
|
||||
headers.set("Authorization", `Bearer ${token}`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,10 @@ import {
|
|||
} from "@clerk/nextjs";
|
||||
|
||||
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
|
||||
import { getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
|
||||
import { getEnvToken, getLocalAuthToken, isLocalAuthMode } from "@/auth/localAuth";
|
||||
|
||||
function hasLocalAuthToken(): boolean {
|
||||
return Boolean(getLocalAuthToken());
|
||||
return Boolean(getLocalAuthToken() ?? getEnvToken());
|
||||
}
|
||||
|
||||
export function isClerkEnabled(): boolean {
|
||||
|
|
@ -76,7 +76,7 @@ export function useUser() {
|
|||
|
||||
export function useAuth() {
|
||||
if (isLocalAuthMode()) {
|
||||
const token = getLocalAuthToken();
|
||||
const token = getLocalAuthToken() ?? getEnvToken();
|
||||
return {
|
||||
isLoaded: true,
|
||||
isSignedIn: Boolean(token),
|
||||
|
|
|
|||
Loading…
Reference in New Issue