Update Add user hospital portal

This commit is contained in:
ivan-sim
2024-05-20 11:33:48 +07:00
parent f83a7954aa
commit 6c2a4dae1b
14 changed files with 217 additions and 96 deletions

View File

@@ -2,7 +2,7 @@ import { createContext, ReactNode, useEffect, useReducer } from 'react';
// utils
import axios from '@/utils/axios';
// import { isValidToken, setSession } from '@/utils/jwt';
import { setSession, getSession, setUser, getUser } from '@/utils/token';
import { setSession, getSession, setUser, getUser, getCookie } from '@/utils/token';
// @types
import { ActionMap, AuthState, AuthUser, JWTContextType } from '@/@types/auth';
// ----------------------------------------------------------------------
@@ -86,12 +86,16 @@ function AuthProvider({ children }: AuthProviderProps) {
const initialize = async () => {
try {
const accessToken = getSession();
if (accessToken) {
setSession(accessToken);
const rememberMe = getCookie('rememberMe') == 'OK' ? false : true;
if (accessToken) {
const userString = getUser();
const storedUser = userString ? JSON.parse(userString) : null;
setUser(storedUser, rememberMe);
setSession(accessToken, rememberMe);
const response = await axios.get('/user');
const user = response.data;
const response = await axios.get('/user');
const user = response.data;
dispatch({
type: Types.Initial,
payload: {
@@ -126,16 +130,16 @@ function AuthProvider({ children }: AuthProviderProps) {
headers: {
'Accept': 'application/json',
'Content-Type' : 'application/json',
'Accept-Language': (localStorage.getItem('currentLocale') ? localStorage.getItem('currentLocale') : 'id-ID'),
'Accept-Language': localStorage.getItem('currentLocale') ?? 'id-ID',
},
};
const login = async (email: string, password: string) => axios
const login = async (email: string, password: string, rememberMe: boolean) => axios
.post('/login', { email, password }, headers)
.then((response) => {
const { user, token } = response.data.data;
setSession(token);
setUser(user);
setSession(token, rememberMe);
setUser(user, rememberMe);
dispatch({
type: Types.Login,
@@ -168,8 +172,9 @@ function AuthProvider({ children }: AuthProviderProps) {
};
const logout = async () => {
setSession(null);
setUser(null);
await axios.post('logout');
setSession(null, false);
setUser(null, false);
dispatch({ type: Types.Logout });
};
@@ -187,9 +192,9 @@ function AuthProvider({ children }: AuthProviderProps) {
);
// if (state.isInitialized) {
// return (!state.isAuthenticated && location.pathname !== '/auth/login') ?
// return (!state.isAuthenticated && location.pathname !== '/auth/login') ?
// (<Navigate to="/auth/login" replace={true} />)
// : false && location.pathname == '/auth/login' ?
// : false && location.pathname == '/auth/login' ?
// (<Navigate to="/dashboard" replace={true} />)
// : (
// <AuthContext.Provider