♻️ refactor(auth): move the auth component to the auth.js file in the helpers folder
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import React, { lazy, Suspense, useContext, useEffect } from 'react';
|
import React, { lazy, Suspense } from 'react';
|
||||||
import { Route, Routes, useLocation } from 'react-router-dom';
|
import { Route, Routes, useLocation } from 'react-router-dom';
|
||||||
import Loading from './components/Loading';
|
import Loading from './components/Loading';
|
||||||
import User from './pages/User';
|
import User from './pages/User';
|
||||||
import { PrivateRoute } from './components/PrivateRoute';
|
import { AuthRedirect, PrivateRoute } from './helpers';
|
||||||
import RegisterForm from './components/RegisterForm';
|
import RegisterForm from './components/RegisterForm';
|
||||||
import LoginForm from './components/LoginForm';
|
import LoginForm from './components/LoginForm';
|
||||||
import NotFound from './pages/NotFound';
|
import NotFound from './pages/NotFound';
|
||||||
@@ -18,7 +18,6 @@ import TopUp from './pages/TopUp';
|
|||||||
import Log from './pages/Log';
|
import Log from './pages/Log';
|
||||||
import Chat from './pages/Chat';
|
import Chat from './pages/Chat';
|
||||||
import Chat2Link from './pages/Chat2Link';
|
import Chat2Link from './pages/Chat2Link';
|
||||||
import { Layout } from '@douyinfe/semi-ui';
|
|
||||||
import Midjourney from './pages/Midjourney';
|
import Midjourney from './pages/Midjourney';
|
||||||
import Pricing from './pages/Pricing/index.js';
|
import Pricing from './pages/Pricing/index.js';
|
||||||
import Task from './pages/Task/index.js';
|
import Task from './pages/Task/index.js';
|
||||||
@@ -27,7 +26,6 @@ import OAuth2Callback from './components/OAuth2Callback.js';
|
|||||||
import PersonalSetting from './components/PersonalSetting.js';
|
import PersonalSetting from './components/PersonalSetting.js';
|
||||||
import Setup from './pages/Setup/index.js';
|
import Setup from './pages/Setup/index.js';
|
||||||
import SetupCheck from './components/SetupCheck';
|
import SetupCheck from './components/SetupCheck';
|
||||||
import AuthRedirect from './components/AuthRedirect';
|
|
||||||
|
|
||||||
const Home = lazy(() => import('./pages/Home'));
|
const Home = lazy(() => import('./pages/Home'));
|
||||||
const Detail = lazy(() => import('./pages/Detail'));
|
const Detail = lazy(() => import('./pages/Detail'));
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { Navigate } from 'react-router-dom';
|
|
||||||
|
|
||||||
const AuthRedirect = ({ children }) => {
|
|
||||||
const user = localStorage.getItem('user');
|
|
||||||
|
|
||||||
if (user) {
|
|
||||||
return <Navigate to="/console" replace />;
|
|
||||||
}
|
|
||||||
|
|
||||||
return children;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default AuthRedirect;
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { Navigate } from 'react-router-dom';
|
|
||||||
|
|
||||||
import { history } from '../helpers';
|
|
||||||
|
|
||||||
function PrivateRoute({ children }) {
|
|
||||||
if (!localStorage.getItem('user')) {
|
|
||||||
return <Navigate to='/login' state={{ from: history.location }} />;
|
|
||||||
}
|
|
||||||
return children;
|
|
||||||
}
|
|
||||||
|
|
||||||
export { PrivateRoute };
|
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { Navigate } from 'react-router-dom';
|
||||||
|
import { history } from './history';
|
||||||
|
|
||||||
export function authHeader() {
|
export function authHeader() {
|
||||||
// return authorization header with jwt token
|
// return authorization header with jwt token
|
||||||
let user = JSON.parse(localStorage.getItem('user'));
|
let user = JSON.parse(localStorage.getItem('user'));
|
||||||
@@ -8,3 +12,22 @@ export function authHeader() {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const AuthRedirect = ({ children }) => {
|
||||||
|
const user = localStorage.getItem('user');
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
return <Navigate to="/console" replace />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return children;
|
||||||
|
};
|
||||||
|
|
||||||
|
function PrivateRoute({ children }) {
|
||||||
|
if (!localStorage.getItem('user')) {
|
||||||
|
return <Navigate to='/login' state={{ from: history.location }} />;
|
||||||
|
}
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { PrivateRoute };
|
||||||
|
|||||||
Reference in New Issue
Block a user