22 lines
560 B
TypeScript
22 lines
560 B
TypeScript
|
|
import { create } from 'zustand';
|
||
|
|
|
||
|
|
interface AuthState {
|
||
|
|
token: string | null;
|
||
|
|
isAuthenticated: boolean;
|
||
|
|
setToken: (token: string) => void;
|
||
|
|
logout: () => void;
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useAuthStore = create<AuthState>((set) => ({
|
||
|
|
token: localStorage.getItem('token'),
|
||
|
|
isAuthenticated: !!localStorage.getItem('token'),
|
||
|
|
setToken: (token: string) => {
|
||
|
|
localStorage.setItem('token', token);
|
||
|
|
set({ token, isAuthenticated: true });
|
||
|
|
},
|
||
|
|
logout: () => {
|
||
|
|
localStorage.removeItem('token');
|
||
|
|
set({ token: null, isAuthenticated: false });
|
||
|
|
},
|
||
|
|
}));
|