gitscope
← marzneshin / v0.3.0-alpha7 / dashboard/src/features/auth/auth.store.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/auth/auth.store.ts · TypeScript
Blame Raw
 1import { fetch } from '@marzneshin/utils';
 2import { create } from 'zustand';
 3import { subscribeWithSelector } from 'zustand/middleware';
 4
 5type AdminStateType = {
 6    isLoggedIn: () => Promise<boolean>,
 7    getAuthToken: () => string | null;
 8    setAuthToken: (token: string) => void;
 9    removeAuthToken: () => void;
10}
11
12export const useAuth = create(
13    subscribeWithSelector<AdminStateType>(() => ({
14        isLoggedIn: async () => {
15            try {
16                await fetch('/admins');
17                return true;
18            } catch (error) {
19                return false;
20            }
21        },
22        getAuthToken: () => {
23            return localStorage.getItem('token');
24        },
25        setAuthToken: (token: string) => {
26            localStorage.setItem('token', token);
27        },
28        removeAuthToken: () => {
29            localStorage.removeItem('token');
30        },
31    }))
32)