mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-28 22:17:48 +02:00
Desktop Google login flow: click "Continue with Google" → opens default browser to web login page with platform=desktop → Google OAuth completes → web callback redirects to multica://auth/callback?token=<jwt> → Electron receives deep link, extracts token, completes login. Changes: - Register `multica://` protocol in Electron (main process + builder) - Add single-instance lock with deep link forwarding (macOS + Win/Linux) - Expose `desktopAPI.onAuthToken` and `openExternal` via preload IPC - Add `loginWithToken(token)` to core auth store - Pass `state=platform:desktop` through Google OAuth flow - Web callback detects desktop state and redirects via deep link - Desktop renderer listens for auth token and hydrates session
120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
import { create } from "zustand";
|
|
import type { User, StorageAdapter } from "../types";
|
|
import type { ApiClient } from "../api/client";
|
|
|
|
export interface AuthStoreOptions {
|
|
api: ApiClient;
|
|
storage: StorageAdapter;
|
|
onLogin?: () => void;
|
|
onLogout?: () => void;
|
|
/** When true, rely on HttpOnly cookies instead of localStorage for auth tokens. */
|
|
cookieAuth?: boolean;
|
|
}
|
|
|
|
export interface AuthState {
|
|
user: User | null;
|
|
isLoading: boolean;
|
|
|
|
initialize: () => Promise<void>;
|
|
sendCode: (email: string) => Promise<void>;
|
|
verifyCode: (email: string, code: string) => Promise<User>;
|
|
loginWithGoogle: (code: string, redirectUri: string) => Promise<User>;
|
|
loginWithToken: (token: string) => Promise<User>;
|
|
logout: () => void;
|
|
setUser: (user: User) => void;
|
|
}
|
|
|
|
export function createAuthStore(options: AuthStoreOptions) {
|
|
const { api, storage, onLogin, onLogout, cookieAuth } = options;
|
|
|
|
return create<AuthState>((set) => ({
|
|
user: null,
|
|
isLoading: true,
|
|
|
|
initialize: async () => {
|
|
if (cookieAuth) {
|
|
// In cookie mode, the HttpOnly cookie is sent automatically.
|
|
// Try to fetch the current user — if the cookie exists the server will accept it.
|
|
try {
|
|
const user = await api.getMe();
|
|
set({ user, isLoading: false });
|
|
} catch {
|
|
set({ user: null, isLoading: false });
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Token mode: read from localStorage (Electron / legacy).
|
|
const token = storage.getItem("multica_token");
|
|
if (!token) {
|
|
set({ isLoading: false });
|
|
return;
|
|
}
|
|
|
|
api.setToken(token);
|
|
|
|
try {
|
|
const user = await api.getMe();
|
|
set({ user, isLoading: false });
|
|
} catch {
|
|
api.setToken(null);
|
|
api.setWorkspaceId(null);
|
|
storage.removeItem("multica_token");
|
|
set({ user: null, isLoading: false });
|
|
}
|
|
},
|
|
|
|
sendCode: async (email: string) => {
|
|
await api.sendCode(email);
|
|
},
|
|
|
|
verifyCode: async (email: string, code: string) => {
|
|
const { token, user } = await api.verifyCode(email, code);
|
|
if (!cookieAuth) {
|
|
// Token mode: persist for Electron / legacy.
|
|
storage.setItem("multica_token", token);
|
|
api.setToken(token);
|
|
}
|
|
onLogin?.();
|
|
set({ user });
|
|
return user;
|
|
},
|
|
|
|
loginWithGoogle: async (code: string, redirectUri: string) => {
|
|
const { token, user } = await api.googleLogin(code, redirectUri);
|
|
if (!cookieAuth) {
|
|
storage.setItem("multica_token", token);
|
|
api.setToken(token);
|
|
}
|
|
onLogin?.();
|
|
set({ user });
|
|
return user;
|
|
},
|
|
|
|
loginWithToken: async (token: string) => {
|
|
storage.setItem("multica_token", token);
|
|
api.setToken(token);
|
|
const user = await api.getMe();
|
|
onLogin?.();
|
|
set({ user, isLoading: false });
|
|
return user;
|
|
},
|
|
|
|
logout: () => {
|
|
if (cookieAuth) {
|
|
// Clear server-side HttpOnly cookie.
|
|
api.logout().catch(() => {});
|
|
}
|
|
storage.removeItem("multica_token");
|
|
api.setToken(null);
|
|
api.setWorkspaceId(null);
|
|
onLogout?.();
|
|
set({ user: null });
|
|
},
|
|
|
|
setUser: (user: User) => {
|
|
set({ user });
|
|
},
|
|
}));
|
|
}
|