45 lines
836 B
TypeScript
45 lines
836 B
TypeScript
import { create } from "zustand";
|
|
import { persist } from "zustand/middleware";
|
|
|
|
export interface AuthData {
|
|
ok: number;
|
|
token: string;
|
|
}
|
|
|
|
export interface AccountData {
|
|
ok: number;
|
|
username: string;
|
|
}
|
|
|
|
|
|
export interface AuthState {
|
|
token: string | null;
|
|
setToken: (token: string) => void;
|
|
clearToken: () => void;
|
|
checkToken: (token: string) => Promise<boolean>;
|
|
}
|
|
|
|
export const useAuthStore = create<AuthState>()(
|
|
persist(
|
|
(set) => ({
|
|
token: null,
|
|
setToken: (token) => set({ token }),
|
|
clearToken: () => set({ token: null }),
|
|
checkToken: async (token: string) => {
|
|
const res = await fetch("/api/auth/check", {
|
|
method: "GET",
|
|
mode: "same-origin",
|
|
headers: {
|
|
"Authorization": `Basic ${token}`
|
|
}
|
|
});
|
|
|
|
return res.status === 200;
|
|
}
|
|
}),
|
|
{
|
|
name: "auth-storage"
|
|
}
|
|
)
|
|
);
|
|
|