Fix Authentication

This commit is contained in:
2022-05-26 11:38:45 +07:00
parent d2ebf09e48
commit 12c298b9a0
4 changed files with 24 additions and 16 deletions

View File

@@ -87,7 +87,8 @@ function AuthProvider({ children }: AuthProviderProps) {
console.log('initialize', state)
try {
const accessToken = getSession();
console.log('')
if (accessToken) {
setSession(accessToken);
@@ -111,7 +112,6 @@ function AuthProvider({ children }: AuthProviderProps) {
});
}
} catch (err) {
console.error(err);
dispatch({
type: Types.Initial,
payload: {
@@ -124,7 +124,6 @@ function AuthProvider({ children }: AuthProviderProps) {
initialize();
}, []);
// const csrf = () => axios.get('/sanctum/csrf-cookie')
const login = async (email: string, password: string) => {
return axios
@@ -169,8 +168,7 @@ function AuthProvider({ children }: AuthProviderProps) {
dispatch({ type: Types.Logout });
};
return (
<AuthContext.Provider
return (<AuthContext.Provider
value={{
...state,
method: 'jwt',

View File

@@ -26,7 +26,7 @@ export default function AuthGuard({ children }: AuthGuardProps) {
if (pathname !== requestedLocation) {
setRequestedLocation(pathname);
}
return <Login />;
return <Navigate to="/auth/login" replace={true}/>;
}
if (requestedLocation && pathname !== requestedLocation) {

View File

@@ -15,7 +15,7 @@ export default function GuestGuard({ children }: GuestGuardProps) {
const { isAuthenticated } = useAuth();
if (isAuthenticated) {
return <Navigate to={'/dashboard'} />;
return <Navigate to={'/dashboard'} replace={true}/>;
}
return <>{children}</>;

View File

@@ -11,6 +11,7 @@ import Register from '../pages/auth/Register';
import ResetPassword from '../pages/auth/ResetPassword';
import VerifyCode from '../pages/auth/VerifyCode';
import { AuthProvider } from '../contexts/LaravelAuthContext';
import AuthGuard from '../guards/AuthGuard';
// ----------------------------------------------------------------------
@@ -34,22 +35,26 @@ export default function Router() {
path: 'login',
element: (
<AuthProvider>
<Login />
<GuestGuard>
<Login />
</GuestGuard>
</AuthProvider>
),
},
{
path: 'register',
element: (
<GuestGuard>
<RegisterForm />
</GuestGuard>
<AuthProvider>
<GuestGuard>
<RegisterForm />
</GuestGuard>
</AuthProvider>
),
},
{ path: 'login-unprotected', element: <Login /> },
{ path: 'register-unprotected', element: <Register /> },
{ path: 'reset-password', element: <ResetPassword /> },
{ path: 'verify', element: <VerifyCode /> },
// { path: 'login-unprotected', element: <Login /> },
// { path: 'register-unprotected', element: <Register /> },
// { path: 'reset-password', element: <ResetPassword /> },
// { path: 'verify', element: <VerifyCode /> },
],
},
// {
@@ -58,7 +63,12 @@ export default function Router() {
// },
{
path: '/',
element: <AuthProvider><DashboardLayout /></AuthProvider>,
element: (
<AuthProvider>
<AuthGuard>
<DashboardLayout />
</AuthGuard>
</AuthProvider>),
children:[
{
path: 'dashboard',