@@ -1,5 +1,6 @@
|
||||
import type { AuthData } from '@/data/AuthData';
|
||||
import { create } from 'zustand';
|
||||
import { createJSONStorage, persist } from 'zustand/middleware';
|
||||
|
||||
interface AuthStoreProps {
|
||||
authData: AuthData | undefined;
|
||||
@@ -7,17 +8,23 @@ interface AuthStoreProps {
|
||||
logout: () => void;
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthStoreProps>((set) => ({
|
||||
authData: undefined,
|
||||
login: (data: AuthData) => {
|
||||
set({ authData: data });
|
||||
Object.entries(data)
|
||||
.forEach((entry) => {
|
||||
localStorage.setItem(entry[0], entry[1]);
|
||||
})
|
||||
},
|
||||
logout: () => {
|
||||
set({ authData: undefined });
|
||||
localStorage.clear();
|
||||
}
|
||||
}));
|
||||
const storage = sessionStorage;
|
||||
export const useAuthStore = create<AuthStoreProps>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
authData: undefined,
|
||||
login: (data: AuthData) => {
|
||||
set({ authData: data });
|
||||
},
|
||||
logout: () => {
|
||||
localStorage.setItem('autoLogin', 'false');
|
||||
localStorage.removeItem('auth-storage');
|
||||
set({ authData: undefined });
|
||||
}
|
||||
}),
|
||||
{
|
||||
name: 'auth-storage',
|
||||
storage: createJSONStorage(() => storage)
|
||||
}
|
||||
)
|
||||
);
|
||||
Reference in New Issue
Block a user