fixing so much trouble
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './use-query-state';
|
||||
export * from './use-session';
|
||||
export * from './use-auth-store';
|
||||
export * from './use-modal-login';
|
||||
export * from './use-session';
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { create } from 'zustand';
|
||||
import { TLoginItem } from '@imphnen-frontend-service/service';
|
||||
import { SessionToken } from '../cookies';
|
||||
import { SessionUser } from '../local-storage';
|
||||
|
||||
export enum ESessionStatus {
|
||||
Authenticated = 'authenticated',
|
||||
Authenticating = 'authenticating',
|
||||
Unauthenticated = 'unauthenticated',
|
||||
}
|
||||
|
||||
type SessionState = {
|
||||
isLoading: boolean;
|
||||
session?: TLoginItem;
|
||||
status?: ESessionStatus;
|
||||
setLoading: (val: boolean) => void;
|
||||
setSession: (payload: TLoginItem) => void;
|
||||
clearSession: () => void;
|
||||
};
|
||||
|
||||
export const useAuthStore = create<SessionState>((set) => {
|
||||
const session = SessionToken.get();
|
||||
const user = SessionUser.get();
|
||||
const isAuthenticated = !!session;
|
||||
|
||||
return {
|
||||
isLoading: false,
|
||||
session: isAuthenticated ? { ...session, ...user } : undefined,
|
||||
status: isAuthenticated
|
||||
? ESessionStatus.Authenticated
|
||||
: ESessionStatus.Unauthenticated,
|
||||
setLoading: (val) => set({ isLoading: val }),
|
||||
setSession: (data) => {
|
||||
SessionToken.set({ token: data.token });
|
||||
SessionUser.set(data.user);
|
||||
set({
|
||||
session: data,
|
||||
status: ESessionStatus.Authenticated,
|
||||
});
|
||||
},
|
||||
clearSession: () => {
|
||||
SessionToken.remove();
|
||||
SessionUser.remove();
|
||||
set({ session: undefined, status: ESessionStatus.Unauthenticated });
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -1,22 +1,37 @@
|
||||
import { SessionToken, SessionUser } from '../local-storage';
|
||||
import { TLoginRequest, usePostLogin } from '@imphnen-frontend-service/service';
|
||||
import { useAuthStore } from './';
|
||||
import { toast } from 'sonner';
|
||||
import { useNavigate } from 'react-router';
|
||||
|
||||
export const useSession = () => {
|
||||
const session = {
|
||||
user: SessionUser.get(),
|
||||
token: SessionToken.get(),
|
||||
const navigate = useNavigate();
|
||||
const { mutate, isPending } = usePostLogin();
|
||||
const { setLoading, setSession, clearSession, session } = useAuthStore();
|
||||
const signIn = (payload: TLoginRequest) => {
|
||||
setLoading(true);
|
||||
mutate(payload, {
|
||||
onSuccess: (data) => {
|
||||
toast.success('Login sukses');
|
||||
setSession(data.data);
|
||||
navigate(0);
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(
|
||||
err?.response?.data?.message ??
|
||||
'Terjadi Kesalahan yang tidak diketahui'
|
||||
);
|
||||
clearSession();
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const isAuthenticated = !!session.token?.access_token;
|
||||
|
||||
const signOut = () => {
|
||||
SessionUser.remove();
|
||||
SessionToken.remove();
|
||||
window.location.reload();
|
||||
clearSession();
|
||||
navigate(0);
|
||||
};
|
||||
|
||||
return {
|
||||
isAuthenticated,
|
||||
session,
|
||||
signIn,
|
||||
signOut,
|
||||
isLoading: isPending,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user