Skip to content

Commit

Permalink
[ADD]: us02 logout
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaryoshida committed Nov 23, 2023
1 parent e9f3878 commit 7fe16b7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
1 change: 1 addition & 0 deletions backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ exports.userLogin = async (req, res) => {
res.status(500).json({ error: 'Erro durante o login.' });
}
};

2 changes: 1 addition & 1 deletion backend/controllers/middlewares/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!' });
}
Expand Down
16 changes: 0 additions & 16 deletions backend/views/routes/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 10 additions & 3 deletions frontend/src/pages/Home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -12,8 +13,8 @@ import helpIcon from '../../icon/interrogatorio 1.png';
import logoutIcon from '../../icon/sair-alt 1.png';



const Header = () => {
const navigate = useNavigate();



Expand All @@ -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 (
<Flex
as="header"
Expand Down Expand Up @@ -77,8 +84,8 @@ const Header = () => {
</StyledMenuItem>
<StyledMenuItem>
<StyledImage src={logoutIcon} alt='Logout Icon' />
<button>Sair</button>
</StyledMenuItem>
<button Text="Sair" onClick={handleLogout}></button>
</StyledMenuItem>
</StyledMenuList>
</Menu>
</Box>
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/pages/Signin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7fe16b7

Please sign in to comment.