From 6e7f5a9fb0b38036f9f53e5cca1b9068f0ce7f0f Mon Sep 17 00:00:00 2001 From: Yasmim Rosa Date: Tue, 12 Dec 2023 21:50:49 -0300 Subject: [PATCH] =?UTF-8?q?p=C3=A1ginas=20privadas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/contexts/auth.js | 72 +++++++--------------------- frontend/src/pages/Signin/index.js | 10 ++-- frontend/src/routes/index.js | 77 +++++++++++++++++++++--------- 3 files changed, 77 insertions(+), 82 deletions(-) diff --git a/frontend/src/contexts/auth.js b/frontend/src/contexts/auth.js index 656fb2eb..b7cdeab9 100644 --- a/frontend/src/contexts/auth.js +++ b/frontend/src/contexts/auth.js @@ -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 ( {children} diff --git a/frontend/src/pages/Signin/index.js b/frontend/src/pages/Signin/index.js index 0be50b49..2af56fa8 100644 --- a/frontend/src/pages/Signin/index.js +++ b/frontend/src/pages/Signin/index.js @@ -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(""); @@ -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"); } diff --git a/frontend/src/routes/index.js b/frontend/src/routes/index.js index 601fba6c..d2a3da97 100644 --- a/frontend/src/routes/index.js +++ b/frontend/src/routes/index.js @@ -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"; @@ -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 ? : ; -}; +import SendStudent from "../pages/SendStudents"; const RoutesApp = () => { + const { isAuthenticated } = useAuth(); + return ( - - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - - + + } /> + } /> + } /> + + {/* Utilize o componente Outlet para renderizar rotas aninhadas */} + : + } + /> + : + } + /> + : + } + /> + : + } + /> + : + } + /> + : + } + /> + : + } + /> + ); };