From 7fe16b77cbb11a3174bfc7f9a3fd46546acfcb1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Yoshida?= Date: Wed, 22 Nov 2023 21:33:03 -0300 Subject: [PATCH] [ADD]: us02 logout --- backend/controllers/UserControllers.js | 1 + backend/controllers/middlewares/Auth.js | 2 +- backend/views/routes/Users.js | 16 ---------------- frontend/src/pages/Home/index.js | 13 ++++++++++--- frontend/src/pages/Signin/index.js | 5 +++-- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index c7c0dc41..b66b040b 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -57,3 +57,4 @@ exports.userLogin = async (req, res) => { res.status(500).json({ error: 'Erro durante o login.' }); } }; + diff --git a/backend/controllers/middlewares/Auth.js b/backend/controllers/middlewares/Auth.js index 3b3a3b9e..ca7e9ef3 100644 --- a/backend/controllers/middlewares/Auth.js +++ b/backend/controllers/middlewares/Auth.js @@ -9,7 +9,7 @@ const createToken = (user) => { const validateToken = (req, res, next) => { - const accessToken = req.cookies && req.cookies['access-token']; + const accessToken = req.header("accessToken"); if (!accessToken) { return res.status(400).json({ error: 'Usuário não autenticado!' }); } diff --git a/backend/views/routes/Users.js b/backend/views/routes/Users.js index 9369e4d0..ccbe2d81 100644 --- a/backend/views/routes/Users.js +++ b/backend/views/routes/Users.js @@ -13,20 +13,4 @@ router.get('/profile', validateToken, (req, res) => { router.post('/register', userController.userRegister); router.post('/login', userController.userLogin); -// Rota para logout -router.post('/logout', (req, res) => { - // Lógica para fazer logout do usuário - req.session.destroy((err) => { - if (err) { - return res.status(500).json({ message: 'Erro ao fazer logout' }); - } - res.clearCookie('cookieName'); // Limpeza de cookies, se necessário - return res.status(200).json({ message: 'Logout realizado com sucesso' }); - }); - }); - -router.get('/profile', validateToken, (req, res) => { - res.json('profile'); -}); - module.exports = router; diff --git a/frontend/src/pages/Home/index.js b/frontend/src/pages/Home/index.js index 99957ce2..9cec426f 100644 --- a/frontend/src/pages/Home/index.js +++ b/frontend/src/pages/Home/index.js @@ -2,6 +2,7 @@ import React from "react"; import axios from "axios" import styled from "styled-components"; import { Link } from "react-router-dom"; +import { useNavigate } from "react-router-dom"; import { Flex, Box, Heading, Spacer, Menu, MenuButton, MenuList, MenuItem } from "@chakra-ui/react"; import { Image } from '@chakra-ui/react' import cmtnLogo from '../../img/cmtnLogo.png' @@ -12,8 +13,8 @@ import helpIcon from '../../icon/interrogatorio 1.png'; import logoutIcon from '../../icon/sair-alt 1.png'; - const Header = () => { + const navigate = useNavigate(); @@ -38,6 +39,12 @@ const Header = () => { margin-right: 10px; /* Adiciona um espaçamento entre a imagem e o texto */ `; + const handleLogout = () =>{ + sessionStorage.removeItem("accessToken"); + navigate("/login"); + } + + return ( { - - + + diff --git a/frontend/src/pages/Signin/index.js b/frontend/src/pages/Signin/index.js index 20a41a20..0be50b49 100644 --- a/frontend/src/pages/Signin/index.js +++ b/frontend/src/pages/Signin/index.js @@ -24,12 +24,13 @@ const Signin = () => { const response = await axios.post("http://localhost:3001/auth/login", { email: email, password: senha, - }); + }) if (response.data.accessToken) { signin(response.data.accessToken); navigate("/home"); - } else { + sessionStorage.setItem("accessToken", response.data.accessToken); + } else { setError("Credenciais inválidas"); } } catch (error) {