client portal login api

This commit is contained in:
Muhammad Fajar
2022-11-11 08:35:43 +07:00
parent 174f065191
commit 3e30ce6e90
11 changed files with 226 additions and 155 deletions

View File

@@ -80,15 +80,15 @@ type AuthProviderProps = {
function AuthProvider({ children }: AuthProviderProps) {
const [state, dispatch] = useReducer(JWTReducer, initialState);
let location = useLocation();
// let location = useLocation();
useEffect(() => {
const initialize = async () => {
console.log('initialize', state)
console.log('initialize', state);
try {
const accessToken = getSession();
console.log('')
console.log('');
if (accessToken) {
setSession(accessToken);
@@ -125,56 +125,56 @@ function AuthProvider({ children }: AuthProviderProps) {
initialize();
}, []);
const login = async (email: string, password: string) => {
return axios
.post('/login', { email, password })
.then((response) => {
const { user, token } = response.data;
setSession(token);
// const login = async (phone_or_email: string) => {
// axios
// .post('/otp-request', { phone_or_email })
// .then((response: any) => {
// const { user, token } = response.data;
// setSession(token);
dispatch({
type: Types.Login,
payload: {
user,
}
});
})
.catch(error => {
if (error.response.status !== 404) throw error.response
if (error.response.status !== 422) throw error.response
})
};
// dispatch({
// type: Types.Login,
// payload: {
// user,
// },
// });
// })
// .catch((error) => {
// if (error.response.status !== 404) throw error.response;
// if (error.response.status !== 422) throw error.response;
// });
// };
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;
// 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,
},
});
};
// window.localStorage.setItem('accessToken', accessToken);
// dispatch({
// type: Types.Register,
// payload: {
// user,
// },
// });
// };
const logout = async () => {
await axios.post('/logout', { token: getSession() });
setSession(null);
dispatch({ type: Types.Logout });
};
return (<AuthContext.Provider
return (
<AuthContext.Provider
value={{
...state,
method: 'jwt',
login,
logout,
register,
}}
>
{children}
@@ -182,9 +182,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