logged in required
This commit is contained in:
parent
9e9a4b3897
commit
fc942db1a1
@ -6,15 +6,16 @@ import Playlists from '../pages/playlists';
|
|||||||
import Playlist from '../pages/playlist';
|
import Playlist from '../pages/playlist';
|
||||||
import Files from '../pages/files';
|
import Files from '../pages/files';
|
||||||
import Authentication from '../pages/auth';
|
import Authentication from '../pages/auth';
|
||||||
|
import { LoginRequired } from '../tools/grant-access';
|
||||||
|
|
||||||
const AppRouter = () => {
|
const AppRouter = () => {
|
||||||
return (
|
return (
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<Home />} />
|
<Route path="/" element={<Home />} />
|
||||||
<Route path="/planning" element={<Planning />} />
|
<Route path="/planning" element={<LoginRequired children={<Planning />} />} />
|
||||||
<Route path="/playlists" element={<Playlists />} />
|
<Route path="/playlists" element={<LoginRequired children={<Playlists />} />} />
|
||||||
<Route path="/files" element={<Files />} />
|
<Route path="/files" element={<LoginRequired children={<Files />} />} />
|
||||||
<Route path="/playlist/:id" element={<Playlist />} />
|
<Route path="/playlist/:id" element={<LoginRequired children={<Playlist />} />} />
|
||||||
<Route path="/auth" element={<Authentication />} />
|
<Route path="/auth" element={<Authentication />} />
|
||||||
<Route path="*" element={<NotFound />} />
|
<Route path="*" element={<NotFound />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
|
@ -6,10 +6,11 @@ import { useEffect, useState } from 'react';
|
|||||||
import setNotification from './errors/error-notification';
|
import setNotification from './errors/error-notification';
|
||||||
import API from '../services/api';
|
import API from '../services/api';
|
||||||
|
|
||||||
const Authentication = () => {
|
const Authentication = ({ redirect }) => {
|
||||||
const { user, setUser } = useAuth();
|
const { user, setUser } = useAuth();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [loggedIn, setLoggedIn] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user) navigate('/');
|
if (user) navigate('/');
|
||||||
@ -36,7 +37,11 @@ const Authentication = () => {
|
|||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
setUser(res.data);
|
setUser(res.data);
|
||||||
localStorage.setItem('user', res.data);
|
localStorage.setItem('user', res.data);
|
||||||
navigate('/');
|
if (redirect) {
|
||||||
|
setLoggedIn(true);
|
||||||
|
} else {
|
||||||
|
navigate('/');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setNotification(true, res.message);
|
setNotification(true, res.message);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@ -48,6 +53,8 @@ const Authentication = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (loggedIn) return redirect;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container size={420} my={40}>
|
<Container size={420} my={40}>
|
||||||
<Title
|
<Title
|
||||||
|
@ -1,8 +1,23 @@
|
|||||||
import { useAuth } from './auth-provider';
|
import { useAuth } from './auth-provider';
|
||||||
|
import Authentication from '../pages/auth';
|
||||||
|
|
||||||
|
export const Perm = {
|
||||||
|
CREATE_ROLE: 0,
|
||||||
|
CREATE_PLAYLIST: 1,
|
||||||
|
VIEW_PLAYLIST: 2,
|
||||||
|
OWN_PLAYLIST: 3,
|
||||||
|
EDIT_PLAYLIST: 4,
|
||||||
|
};
|
||||||
|
|
||||||
const GrantAccess = ({ roles, children }) => {
|
const GrantAccess = ({ roles, children }) => {
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
return roles.includes(user) ? children : null;
|
return roles.includes(user) ? children : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const LoginRequired = ({ children }) => {
|
||||||
|
const { user } = useAuth();
|
||||||
|
if (!user) return <Authentication redirect={children} />;
|
||||||
|
else return children;
|
||||||
|
};
|
||||||
|
|
||||||
export default GrantAccess;
|
export default GrantAccess;
|
||||||
|
Loading…
Reference in New Issue
Block a user