Compare commits

...

1 Commits

Author SHA1 Message Date
Jiang Bohan
b33ca0de54 fix(auth): AuthInitializer not supporting cookie auth mode
AuthInitializer only checked for multica_token in localStorage. In
cookie auth mode (introduced by the HttpOnly cookie migration), there
is no localStorage token — so AuthInitializer immediately set the user
to null and triggered a logout redirect on every page load/reload.

Add a cookieAuth code path that calls api.getMe() using the HttpOnly
cookie sent automatically by the browser, matching the auth store's
initialize() logic.

Fixes MUL-705, fixes #864
2026-04-13 19:23:29 +08:00
2 changed files with 25 additions and 3 deletions

View File

@@ -17,15 +17,39 @@ export function AuthInitializer({
onLogin,
onLogout,
storage = defaultStorage,
cookieAuth,
}: {
children: ReactNode;
onLogin?: () => void;
onLogout?: () => void;
storage?: StorageAdapter;
cookieAuth?: boolean;
}) {
const qc = useQueryClient();
useEffect(() => {
const api = getApi();
const wsId = storage.getItem("multica_workspace_id");
if (cookieAuth) {
// Cookie mode: the HttpOnly cookie is sent automatically by the browser.
// Call the API to check if the session is still valid.
Promise.all([api.getMe(), api.listWorkspaces()])
.then(([user, wsList]) => {
onLogin?.();
useAuthStore.setState({ user, isLoading: false });
qc.setQueryData(workspaceKeys.list(), wsList);
useWorkspaceStore.getState().hydrateWorkspace(wsList, wsId);
})
.catch((err) => {
logger.error("cookie auth init failed", err);
onLogout?.();
useAuthStore.setState({ user: null, isLoading: false });
});
return;
}
// Token mode: read from localStorage (Electron / legacy).
const token = storage.getItem("multica_token");
if (!token) {
onLogout?.();
@@ -33,9 +57,7 @@ export function AuthInitializer({
return;
}
const api = getApi();
api.setToken(token);
const wsId = storage.getItem("multica_workspace_id");
Promise.all([api.getMe(), api.listWorkspaces()])
.then(([user, wsList]) => {

View File

@@ -74,7 +74,7 @@ export function CoreProvider({
return (
<QueryProvider>
<AuthInitializer onLogin={onLogin} onLogout={onLogout} storage={storage}>
<AuthInitializer onLogin={onLogin} onLogout={onLogout} storage={storage} cookieAuth={cookieAuth}>
<WSProvider
wsUrl={wsUrl}
authStore={authStore}