This commit is contained in:
grimhilt
2023-08-05 21:14:11 +02:00
parent a84a6f836a
commit d1f737afdd
6 changed files with 47 additions and 23 deletions

View File

@@ -3,13 +3,13 @@ import { createContext, useState, useContext } from 'react';
const AuthContext = createContext();
const AuthProvider = ({ children }) => {
const [role, setRole] = useState(localStorage.getItem('role') ?? 'user');
const [user, setUser] = useState(localStorage.getItem('user'));
return <AuthContext.Provider value={{ role, setRole }}>{children}</AuthContext.Provider>;
return <AuthContext.Provider value={{ user, setUser }}>{children}</AuthContext.Provider>;
};
const logout = () => {
localStorage.removeItem('role');
localStorage.removeItem('user');
window.location.href = '/auth';
};

View File

@@ -1,8 +1,8 @@
import { useAuth } from './auth-provider';
const GrantAccess = ({ roles, children }) => {
const { role } = useAuth();
return roles.includes(role) ? children : null;
const { user } = useAuth();
return roles.includes(user) ? children : null;
};
export default GrantAccess;