Skip to content

Commit

Permalink
rotas por tipo de usuário
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 13, 2023
1 parent 81dbb8b commit b677840
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ exports.userLogin = async (req, res) => {
secure: true,
});

res.json({ accessToken: accessToken });
res.json({ accessToken: accessToken, superuser: user.superuser });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Erro durante o login.' });
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/contexts/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ export const AuthProvider = ({ children }) => {

const signout = () => {
sessionStorage.removeItem("accessToken");
sessionStorage.removeItem("superuser");
};

const isAuthenticated = () => {
return signin()
}

const isSuperUser = () => {
const userType = sessionStorage.getItem("superuser");
return userType
}


return (
<AuthContext.Provider
value={{signin, isAuthenticated}}
value={{signin, isAuthenticated, isSuperUser}}
>
{children}
</AuthContext.Provider>
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/pages/Signin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Text } from '@chakra-ui/react';
import axios from 'axios';

const Signin = () => {
const { signin, isAuthenticated } = useAuth();
const { signin, isSuperUser } = useAuth();
const navigate = useNavigate();

const [email, setEmail] = useState("");
Expand All @@ -28,9 +28,12 @@ const Signin = () => {

if (response.data.accessToken) {
sessionStorage.setItem("accessToken", response.data.accessToken);
sessionStorage.setItem("user", JSON.stringify({email, senha}));
sessionStorage.setItem("superuser", response.data.superuser);

signin();

console.log("oi")
console.log( sessionStorage.getItem("superuser"))
console.log(isSuperUser())
navigate("/home");


Expand Down
103 changes: 57 additions & 46 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,72 @@ import Recommendations from "../pages/Recommendations";
import SendStudent from "../pages/SendStudents";

const RoutesApp = () => {
const { isAuthenticated } = useAuth();
const { isAuthenticated, isSuperUser } = useAuth();

return (
<BrowserRouter>
<Routes>
<Route path="/signin" element={<Signin />} />
<Route path="/signup" element={<Signup />} />
<Route path="/" element={<Navigate to="/signin" />} />

{/* Utilize o componente Outlet para renderizar rotas aninhadas */}
<Route
path="/home"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <Home />
}
/>
<Route
path="/criar-eletiva"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <CreateEletivas />
}
/>
<Route
path="/excluir-eletivas"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <ExclusionEletivas />
}
/>
<Route
path="/criar-trilha"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <CreateTrilhas />
}
/>
<Route
path="/excluir-trilha"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <ExclusionTrilhas />
}
/>
<Route
path="/recommendations"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <Recommendations />
}
/>
<Route
path="/cadastrar-estudantes"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <SendStudent />
}
/>

{!isSuperUser() ? (
<>
<Route
path="/home"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <Home />
}
/>
<Route
path="/cadastrar-estudantes"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <SendStudent />
}
/>
<Route
path="/criar-eletiva"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <CreateEletivas />
}
/>
<Route
path="/excluir-eletivas"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <ExclusionEletivas />
}
/>
<Route
path="/criar-trilha"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <CreateTrilhas />
}
/>
<Route
path="/excluir-trilha"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <ExclusionTrilhas />
}
/>
<Route
path="/recommendations"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <Recommendations />
}
/>
</>
) : (
<Route
path="/home"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <Home />
}
/>
)}
</Routes>
</BrowserRouter>
);
};
}

export default RoutesApp;

0 comments on commit b677840

Please sign in to comment.