Skip to content

Commit

Permalink
páginas privadas
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 13, 2023
1 parent d2a51f1 commit 6e7f5a9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 82 deletions.
72 changes: 16 additions & 56 deletions frontend/src/contexts/auth.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,32 @@
import { stubFalse } from "lodash";
import { createContext, useEffect, useState } from "react";
import Signin from "../pages/Signin";

export const AuthContext = createContext({});

export const AuthProvider = ({ children }) => {
const [user, setUser] = useState();

useEffect(() => {
const userToken = localStorage.getItem("user_token");
const usersStorage = localStorage.getItem("users_bd");

if (userToken && usersStorage) {
const hasUser = JSON.parse(usersStorage)?.filter(
(user) => user.email === JSON.parse(userToken).email
);

if (hasUser) setUser(hasUser[0]);
}
}, []);

const signin = (email, password) => {
const usersStorage = JSON.parse(localStorage.getItem("users_bd"));

const hasUser = usersStorage?.filter((user) => user.email === email);

if (hasUser?.length) {
if (hasUser[0].email === email && hasUser[0].password === password) {
const token = Math.random().toString(36).substring(2);
localStorage.setItem("user_token", JSON.stringify({ email, token }));
setUser({ email, password });
return;
} else {
return "E-mail ou senha incorretos";
}
} else {
return "Usuário não cadastrado";

const signin = () => {
const userToken = sessionStorage.getItem("accessToken");

if (userToken) {
return true;
}
};

const signup = (email, password) => {
const usersStorage = JSON.parse(localStorage.getItem("users_bd"));

const hasUser = usersStorage?.filter((user) => user.email === email);

if (hasUser?.length) {
return "Já tem uma conta com esse E-mail";
}

let newUser;

if (usersStorage) {
newUser = [...usersStorage, { email, password }];
} else {
newUser = [{ email, password }];
}

localStorage.setItem("users_bd", JSON.stringify(newUser));

return;
return false;
};

const signout = () => {
setUser(null);
localStorage.removeItem("user_token");
sessionStorage.removeItem("accessToken");
};

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


return (
<AuthContext.Provider
value={{ user, signed: !!user, signin, signup, signout }}
value={{signin, isAuthenticated}}
>
{children}
</AuthContext.Provider>
Expand Down
10 changes: 7 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 } = useAuth();
const { signin, isAuthenticated } = useAuth();
const navigate = useNavigate();

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

if (response.data.accessToken) {
signin(response.data.accessToken);
navigate("/home");
sessionStorage.setItem("accessToken", response.data.accessToken);
sessionStorage.setItem("user", JSON.stringify({email, senha}));
signin();

navigate("/home");


} else {
setError("Credenciais inválidas");
}
Expand Down
77 changes: 54 additions & 23 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fragment } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import React from "react";
import { Navigate, Route, BrowserRouter, Routes } from "react-router-dom";
import useAuth from "../hooks/useAuth";
import Home from "../pages/Home";
import Signin from "../pages/Signin";
Expand All @@ -9,31 +9,62 @@ import CreateEletivas from "../pages/CreateEletivas";
import CreateTrilhas from "../pages/CreateTrilhas";
import ExclusionTrilhas from "../pages/ExclusionTrilhas";
import Recommendations from "../pages/Recommendations";
import SendStudent from "../pages/SendStudents"


const Private = ({ Item }) => {
const { signed } = useAuth();

return signed > 0 ? <Item /> : <Signin />;
};
import SendStudent from "../pages/SendStudents";

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

return (
<BrowserRouter>
<Fragment>
<Routes>
<Route path="/home" element={<Home />} />
<Route path="/" element={<Signin />} />
<Route path="/signup" element={<Signup />} />
<Route path="/criar-eletiva" element={<CreateEletivas />} />
<Route path="/excluir-eletivas" element={<ExclusionEletivas />} />
<Route path="/criar-trilha" element={<CreateTrilhas />} />
<Route path="/excluir-trilha" element={<ExclusionTrilhas />} />
<Route path="/recommendations" element={<Recommendations />} />
<Route path="/cadastrar-estudantes" element={<SendStudent />} />
</Routes>
</Fragment>
<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 />
}
/>
</Routes>
</BrowserRouter>
);
};
Expand Down

0 comments on commit 6e7f5a9

Please sign in to comment.