diff --git a/backend/config/config.json b/backend/config/config.json index 14e95799..b8618e86 100644 --- a/backend/config/config.json +++ b/backend/config/config.json @@ -20,4 +20,4 @@ "host": "127.0.0.1", "dialect": "mysql" } -} +} \ No newline at end of file diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index b66b040b..6a7ca717 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -58,3 +58,16 @@ exports.userLogin = async (req, res) => { } }; +exports.userLogout = async (req, res) => { + try { + const { accessToken } = req.body; + await Users.update( + { token: null }, + { where: { token: accessToken } } + ); + res.json({ message: 'Logout bem-sucedido' }); + } catch (err) { + console.error(err); + res.status(500).json({ error: 'Erro durante o logout.' }); + } +}; \ No newline at end of file diff --git a/backend/views/routes/Users.js b/backend/views/routes/Users.js index ccbe2d81..d9bed33f 100644 --- a/backend/views/routes/Users.js +++ b/backend/views/routes/Users.js @@ -12,5 +12,6 @@ router.get('/profile', validateToken, (req, res) => { router.post('/register', userController.userRegister); router.post('/login', userController.userLogin); +router.post('/logout', userController.userLogout); module.exports = router; diff --git a/frontend/package.json b/frontend/package.json index 61ff5b78..04bae6f3 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -10,13 +10,15 @@ "@testing-library/react": "^13.0.0", "@testing-library/user-event": "^13.2.1", "axios": "^1.6.2", + "formik": "^2.4.5", "framer-motion": "^10.16.4", "react": "^18.0.0", "react-dom": "^18.0.0", "react-router-dom": "^6.3.0", "react-scripts": "^5.0.1", "styled-components": "^5.3.5", - "web-vitals": "^2.1.0" + "web-vitals": "^2.1.0", + "yup": "^1.3.2" }, "scripts": { "start": "react-scripts start", diff --git a/frontend/src/components/Header/index.js b/frontend/src/components/Header/index.js index d6702a01..5e43ef24 100644 --- a/frontend/src/components/Header/index.js +++ b/frontend/src/components/Header/index.js @@ -17,19 +17,12 @@ const Header = () => { const handleLogout = async () => { const accessToken = sessionStorage.getItem("accessToken"); - try { - // Chamar o backend para remover o token no banco de dados await axios.post('http://localhost:3001/auth/logout', { accessToken }); - - // Remover o token da sessionStorage sessionStorage.removeItem("accessToken"); - - // Redirecionar para a página de login navigate("/login"); } catch (error) { console.error("Erro ao fazer logout:", error.message); - // Lidar com erros, se necessário } };