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

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

View File

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

View File

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

View File

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