gseps-front/src/stores/useAuthStore.ts

54 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-08-23 08:12:52 +00:00
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
export interface ProfileType {
id: string;
sub: string;
iat: number;
exp: number;
}
/**
* Auth State Type
*/
export interface AuthStateType {
loggedIn: boolean;
email: string | null;
profile: ProfileType | null;
}
/**
* Auth Action Type
*/
export interface AuthActionType {
setSignIn: (by: ProfileType) => void;
setEmail: (by: string) => void;
setSignOut: () => void;
}
export type AuthStoreType = AuthStateType & AuthActionType;
export const useAuthStore = create<AuthStoreType>()(
devtools(
(set): AuthStoreType => ({
loggedIn: false,
email: null,
profile: null,
setSignIn: (by: ProfileType) =>
set(() => ({
profile: { ...by },
loggedIn: true,
})),
setEmail: (by: string) => set(() => ({ email: by })),
setSignOut: () =>
set(() => ({
loggedIn: false,
email: null,
})),
}),
{ name: 'auth' },
),
);