Initial commit: GovAI 政务AI平台
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { create } from "zustand";
|
||||
import api from "@/lib/api";
|
||||
import type { User, Organization } from "@/lib/types";
|
||||
|
||||
export type { Organization };
|
||||
|
||||
interface AuthState {
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
_hasFetched: boolean;
|
||||
fetchUser: () => Promise<void>;
|
||||
login: (email: string, password: string, orgId?: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
setAuth: (user: User, token: string) => void;
|
||||
switchOrg: (orgId: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
user: null,
|
||||
isLoading: true,
|
||||
isAuthenticated: false,
|
||||
_hasFetched: false,
|
||||
|
||||
fetchUser: async () => {
|
||||
if (get()._hasFetched) return;
|
||||
set({ _hasFetched: true });
|
||||
|
||||
const token = typeof window !== "undefined" ? localStorage.getItem("token") : null;
|
||||
if (!token) {
|
||||
set({ user: null, isAuthenticated: false, isLoading: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||
|
||||
try {
|
||||
const user = await api.get<User>("/api/v1/auth/me", { signal: controller.signal });
|
||||
set({ user, isAuthenticated: true, isLoading: false });
|
||||
} catch {
|
||||
if (typeof window !== "undefined") localStorage.removeItem("token");
|
||||
set({ user: null, isAuthenticated: false, isLoading: false });
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
},
|
||||
|
||||
login: async (email: string, password: string, orgId?: string) => {
|
||||
const data = await api.post<{ user: User; access_token: string }>("/api/v1/auth/login", {
|
||||
email,
|
||||
password,
|
||||
org_id: orgId,
|
||||
});
|
||||
if (data.access_token) {
|
||||
localStorage.setItem("token", data.access_token);
|
||||
}
|
||||
set({ user: data.user, isAuthenticated: true, isLoading: false });
|
||||
},
|
||||
|
||||
setAuth: (user: User, token: string) => {
|
||||
if (token) localStorage.setItem("token", token);
|
||||
set({ user, isAuthenticated: true, isLoading: false });
|
||||
},
|
||||
|
||||
logout: async () => {
|
||||
try {
|
||||
await api.post("/api/v1/auth/logout");
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
localStorage.removeItem("token");
|
||||
set({ user: null, isAuthenticated: false, _hasFetched: false });
|
||||
}
|
||||
},
|
||||
|
||||
switchOrg: async (orgId: string) => {
|
||||
const data = await api.post<{
|
||||
message: string;
|
||||
org: Organization;
|
||||
token?: string;
|
||||
user?: User;
|
||||
}>("/api/v1/auth/switch-org", { org_id: orgId });
|
||||
if (data.token) {
|
||||
localStorage.setItem("token", data.token);
|
||||
}
|
||||
if (data.user && data.org) {
|
||||
set({ user: { ...data.user, org_id: orgId, org: data.org }, isAuthenticated: true });
|
||||
} else {
|
||||
const user = get().user;
|
||||
if (user && data.org) {
|
||||
set({ user: { ...user, org_id: orgId, org: data.org } });
|
||||
}
|
||||
}
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user