[WIP] Login Logout

This commit is contained in:
2022-05-26 11:10:50 +07:00
parent 38f794a536
commit f6664b3b77
35 changed files with 1913 additions and 1438 deletions

View File

@@ -2,11 +2,11 @@ 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';
import { setSession, getSession, getUser } from '../utils/token';
// @types
import { ActionMap, AuthState, AuthUser, JWTContextType } from '../@types/auth';
// ----------------------------------------------------------------------
import { Navigate, useLocation } from 'react-router-dom';
enum Types {
Initial = 'INITIALIZE',
@@ -80,25 +80,27 @@ type AuthProviderProps = {
function AuthProvider({ children }: AuthProviderProps) {
const [state, dispatch] = useReducer(JWTReducer, initialState);
let location = useLocation();
useEffect(() => {
const initialize = async () => {
console.log('initialize', state)
try {
const accessToken = getSession();
if (accessToken) {
setSession(accessToken);
// const response = await axios.get('/api/account/my-account');
// const { user } = response.data;
const response = await axios.get('/user');
const user = response.data;
// dispatch({
// type: Types.Initial,
// payload: {
// isAuthenticated: true,
// user,
// },
// });
dispatch({
type: Types.Initial,
payload: {
isAuthenticated: true,
user,
},
});
} else {
dispatch({
type: Types.Initial,
@@ -122,11 +124,10 @@ function AuthProvider({ children }: AuthProviderProps) {
initialize();
}, []);
// const csrf = () => axios.get('/sanctum/csrf-cookie')
const login = async (email: string, password: string) => {
axios
return axios
.post('/login', { email, password })
.then((response) => {
const { user, token } = response.data;
@@ -140,7 +141,8 @@ function AuthProvider({ children }: AuthProviderProps) {
});
})
.catch(error => {
if (error.response.status !== 422) throw error
if (error.response.status !== 404) throw error.response
if (error.response.status !== 422) throw error.response
})
};
@@ -163,7 +165,7 @@ function AuthProvider({ children }: AuthProviderProps) {
};
const logout = async () => {
console.log('LOGOUT CALLEDS NAKJSNDKJASNDKJASDNAKJSND')
console.log('LOGGING OUT')
setSession(null);
dispatch({ type: Types.Logout });
};
@@ -181,6 +183,28 @@ function AuthProvider({ children }: AuthProviderProps) {
{children}
</AuthContext.Provider>
);
// if (state.isInitialized) {
// return (!state.isAuthenticated && location.pathname !== '/auth/login') ?
// (<Navigate to="/auth/login" replace={true} />)
// : false && location.pathname == '/auth/login' ?
// (<Navigate to="/dashboard" replace={true} />)
// : (
// <AuthContext.Provider
// value={{
// ...state,
// method: 'jwt',
// login,
// logout,
// register,
// }}
// >
// {children}
// </AuthContext.Provider>
// );
// } else {
// return (<Navigate to="/auth/login" replace={true} />)
// }
}
export { AuthContext, AuthProvider };