2025-06-04 00:21:03 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
import { Navigate } from 'react-router-dom';
|
|
|
|
|
import { history } from './history';
|
|
|
|
|
|
2023-04-22 20:39:27 +08:00
|
|
|
export function authHeader() {
|
2024-03-23 21:24:39 +08:00
|
|
|
// return authorization header with jwt token
|
|
|
|
|
let user = JSON.parse(localStorage.getItem('user'));
|
2023-04-22 20:39:27 +08:00
|
|
|
|
2024-03-23 21:24:39 +08:00
|
|
|
if (user && user.token) {
|
|
|
|
|
return { Authorization: 'Bearer ' + user.token };
|
|
|
|
|
} else {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2025-06-04 00:21:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 };
|