| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import React from 'react'; |
| import { Navigate } from 'react-router-dom'; |
| import { history } from './history'; |
|
|
| export function authHeader() { |
| |
| let user = JSON.parse(localStorage.getItem('user')); |
|
|
| if (user && user.token) { |
| return { Authorization: 'Bearer ' + user.token }; |
| } else { |
| 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 function AdminRoute({ children }) { |
| const raw = localStorage.getItem('user'); |
| if (!raw) { |
| return <Navigate to='/login' state={{ from: history.location }} />; |
| } |
| try { |
| const user = JSON.parse(raw); |
| if (user && typeof user.role === 'number' && user.role >= 10) { |
| return children; |
| } |
| } catch (e) { |
| |
| } |
| return <Navigate to='/forbidden' replace />; |
| } |
|
|
| export { PrivateRoute }; |
|
|