fix(auth): persist browser session during CLI login flow

When authenticating via CLI, the login page called api.verifyCode()
directly and redirected to the CLI callback without saving the JWT
to localStorage or setting the logged-in cookie. This meant the
browser had no session after CLI login, forcing users to log in
again when visiting multica.ai.

Now the token is saved to localStorage and the cookie is set before
redirecting to the CLI callback, so both CLI and web app share the
same authentication.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jiayuan Zhang 2026-04-08 10:22:19 +08:00
parent 7c79611309
commit 857ec7d4d4
2 changed files with 6 additions and 1 deletions

View file

@ -2,7 +2,7 @@
import { Suspense, useState, useEffect, useCallback } from "react"; import { Suspense, useState, useEffect, useCallback } from "react";
import { useSearchParams, useRouter } from "next/navigation"; import { useSearchParams, useRouter } from "next/navigation";
import { useAuthStore } from "@/features/auth"; import { useAuthStore, setLoggedInCookie } from "@/features/auth";
import { useWorkspaceStore } from "@/features/workspace"; import { useWorkspaceStore } from "@/features/workspace";
import { api } from "@/shared/api"; import { api } from "@/shared/api";
import { import {
@ -146,6 +146,10 @@ function LoginPageContent() {
return; return;
} }
const { token } = await api.verifyCode(email, value); const { token } = await api.verifyCode(email, value);
// Persist session in the browser so the web app stays logged in
localStorage.setItem("multica_token", token);
api.setToken(token);
setLoggedInCookie();
const cliState = searchParams.get("cli_state") || ""; const cliState = searchParams.get("cli_state") || "";
redirectToCliCallback(cliCallback, token, cliState); redirectToCliCallback(cliCallback, token, cliState);
return; return;

View file

@ -1,2 +1,3 @@
export { useAuthStore } from "./store"; export { useAuthStore } from "./store";
export { AuthInitializer } from "./initializer"; export { AuthInitializer } from "./initializer";
export { setLoggedInCookie } from "./auth-cookie";