Separate Client Portal & Dashboard

This commit is contained in:
2022-05-23 10:38:16 +07:00
parent f2e84e6244
commit 89bb57f357
569 changed files with 60252 additions and 280 deletions

View File

@@ -0,0 +1,186 @@
import { createContext, ReactNode, useEffect, useReducer } from 'react';
// utils
import axios from '../utils/axios';
// import { isValidToken, setSession } from '../utils/jwt';
import { setSession, getSession } from '../utils/token';
// @types
import { ActionMap, AuthState, AuthUser, JWTContextType } from '../@types/auth';
// ----------------------------------------------------------------------
enum Types {
Initial = 'INITIALIZE',
Login = 'LOGIN',
Logout = 'LOGOUT',
Register = 'REGISTER',
}
type JWTAuthPayload = {
[Types.Initial]: {
isAuthenticated: boolean;
user: AuthUser;
};
[Types.Login]: {
user: AuthUser;
};
[Types.Logout]: undefined;
[Types.Register]: {
user: AuthUser;
};
};
export type JWTActions = ActionMap<JWTAuthPayload>[keyof ActionMap<JWTAuthPayload>];
const initialState: AuthState = {
isAuthenticated: false,
isInitialized: false,
user: null,
};
const JWTReducer = (state: AuthState, action: JWTActions) => {
switch (action.type) {
case 'INITIALIZE':
return {
isAuthenticated: action.payload.isAuthenticated,
isInitialized: true,
user: action.payload.user,
};
case 'LOGIN':
return {
...state,
isAuthenticated: true,
user: action.payload.user,
};
case 'LOGOUT':
return {
...state,
isAuthenticated: false,
user: null,
};
case 'REGISTER':
return {
...state,
isAuthenticated: true,
user: action.payload.user,
};
default:
return state;
}
};
const AuthContext = createContext<JWTContextType | null>(null);
// ----------------------------------------------------------------------
type AuthProviderProps = {
children: ReactNode;
};
function AuthProvider({ children }: AuthProviderProps) {
const [state, dispatch] = useReducer(JWTReducer, initialState);
useEffect(() => {
const initialize = async () => {
try {
const accessToken = getSession();
if (accessToken) {
setSession(accessToken);
// const response = await axios.get('/api/account/my-account');
// const { user } = response.data;
// dispatch({
// type: Types.Initial,
// payload: {
// isAuthenticated: true,
// user,
// },
// });
} else {
dispatch({
type: Types.Initial,
payload: {
isAuthenticated: false,
user: null,
},
});
}
} catch (err) {
console.error(err);
dispatch({
type: Types.Initial,
payload: {
isAuthenticated: false,
user: null,
},
});
}
};
initialize();
}, []);
// const csrf = () => axios.get('/sanctum/csrf-cookie')
const login = async (email: string, password: string) => {
axios
.post('/login', { email, password })
.then((response) => {
const { user, token } = response.data;
setSession(token);
dispatch({
type: Types.Login,
payload: {
user,
}
});
})
.catch(error => {
if (error.response.status !== 422) throw error
})
};
const register = async (email: string, password: string, firstName: string, lastName: string) => {
const response = await axios.post('/api/register', {
email,
password,
firstName,
lastName,
});
const { accessToken, user } = response.data;
window.localStorage.setItem('accessToken', accessToken);
dispatch({
type: Types.Register,
payload: {
user,
},
});
};
const logout = async () => {
console.log('LOGOUT CALLEDS NAKJSNDKJASNDKJASDNAKJSND')
setSession(null);
dispatch({ type: Types.Logout });
};
return (
<AuthContext.Provider
value={{
...state,
method: 'jwt',
login,
logout,
register,
}}
>
{children}
</AuthContext.Provider>
);
}
export { AuthContext, AuthProvider };