Skip to content

Commit

Permalink
Merge branch 'front-login-integrate' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaryoshida committed Nov 22, 2023
2 parents d0384ab + 3e4d72e commit 1e37d7f
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 163 deletions.
23 changes: 0 additions & 23 deletions backend/config/config.js

This file was deleted.

4 changes: 2 additions & 2 deletions backend/config/config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"development": {
"username": "root",
"password": "password",
"database": "matriculai_development",
"password": "julia12345",
"database": "matriculai",
"host": "localhost",
"dialect": "mysql"
},
Expand Down
22 changes: 10 additions & 12 deletions backend/controllers/ElectiveControllers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { Electives } = require('../models/schemas')


exports.createElective = async(req, res) => {
const { name, description, school_year, teacher, vacancies, schedules } = req.body

const { name, description, school_year, teacher, vacancies, schedules } = req.body;
await Electives.create({
name: name,
description: description,
Expand All @@ -15,18 +13,18 @@ exports.createElective = async(req, res) => {
res.status(201).json("OK")
}).catch((err) => {
if(err){
res.status(400).json({error: err})
}
})
}
res.status(400).json({error: err});
};
});
};

exports.deleteElective = async(req, res) => {
const { id } = req.body
await Electives.destroy({ where: {id: id}}).then(() => {
res.status(200).json("OK")
res.status(200).json("OK");
}).catch((err) => {
if(err){
res.status(400).json({error: err})
}
})
}
res.status(400).json({error: err});
};
});
};
22 changes: 11 additions & 11 deletions backend/controllers/LearningPathsController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { LearningPath } = require('../models/schemas')

exports.createLearningPaths = async(req, res) => {
const { name, description, school_year, electives} = req.body
let electives_object = JSON.stringify(electives)
const { name, description, school_year, electives} = req.body;
let electives_object = JSON.stringify(electives);

await LearningPath.create({
name: name,
Expand All @@ -14,17 +14,17 @@ exports.createLearningPaths = async(req, res) => {
}).catch((err) => {
if(err){
res.status(400).json({error: err})
}
})
}
};
});
};

exports.deleteLearningPaths = async(req, res) => {
const { id } = req.body
const { id } = req.body;
await LearningPath.destroy({ where: {id: id}}).then(() => {
res.status(200).json("OK")
res.status(200).json("OK");
}).catch((err) => {
if(err){
res.status(400).json({error: err})
}
})
}
res.status(400).json({error: err});
};
});
};
87 changes: 49 additions & 38 deletions backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
const { Users } = require('../models/schemas');
const bcrypt = require('bcrypt');
const { sign } = require('jsonwebtoken');
const { createToken, validateToken } = require('./middlewares/Auth');

exports.userRegister = async(req, res) => {
const { email, password } = req.body;
bcrypt.hash(password, 15).then((hash) => {
Users.create({
exports.userRegister = async (req, res) => {
try {
const { email, password } = req.body;
const existingUser = await Users.findOne({ where: { email: email } });

if (existingUser) {
return res.status(400).json({ error: 'E-mail já cadastrado!' });
}

const hash = await bcrypt.hash(password, 15);
await Users.create({
email: email,
password: hash,
}).then(() =>{
res.json('Solicitação bem sucedida!');
}).catch((err) => {
if(err){
res.status(400).json({error: err});
}
});
});

res.json('Solicitação bem sucedida!');
} catch (err) {
res.status(400).json({ error: err.message || 'Erro desconhecido durante o registro.' });
}
};

exports.userLogin = async(req, res) => {
const { email, password } = req.body;
const user = await Users.findOne({where: {email: email}});
if(!user){
res.status(400).json({error: 'E-mail não cadastrado!'});
} else {
bcrypt.compare(password, user.password).then((match) =>{
if(!match){
res.status(400).json({error: 'Senha incorreta!'});
} else {
const accessToken = createToken(user);
res.cookie('access-token', accessToken, {
maxAge: 2592000000,
httpOnly: true,
});
Users.update(
{ token: accessToken },
{ where: { email: user.email } }
).then(() => {
res.json(accessToken);
}).catch((err) => {
res.status(500).json({ error: 'Erro ao atualizar o token no banco de dados.' });
});
};
exports.userLogin = async (req, res) => {
try {
const { email, password } = req.body;
const user = await Users.findOne({ where: { email: email } });

if (!user) {
return res.status(400).json({ error: 'E-mail não cadastrado!' });
}

const match = await bcrypt.compare(password, user.password);

if (!match) {
return res.status(400).json({ error: 'Senha incorreta!' });
}

const accessToken = createToken(user);

await Users.update(
{ token: accessToken },
{ where: { email: user.email } }
);

res.cookie('access-token', accessToken, {
maxAge: 2592000000, // 30 dias em milissegundos
httpOnly: true,
secure: true,
});
};
};

res.json({ accessToken: accessToken });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Erro durante o login.' });
}
};
19 changes: 10 additions & 9 deletions backend/controllers/middlewares/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ const createToken = (user) => {
};

const validateToken = (req, res, next) => {
const accessToken = req.cookies['access-token'];
if(!accessToken) { //vê se o user já foi autenticado pelo cookie de sessão
return res.status(400).json({error: 'Usuário não autenticado!'});
};


const accessToken = req.cookies && req.cookies['access-token'];
if (!accessToken) {
return res.status(400).json({ error: 'Usuário não autenticado!' });
}

try {
const validToken = verify(accessToken, process.env.SECRET);
if(validToken){
if (validToken) {
req.authenticated = true;
return next();
}
} catch(err) {
return res.status(400).json({error: err});
};
} catch (err) {
return res.status(400).json({ error: err.message || 'Erro na validação do token' });
}
};

module.exports = { createToken, validateToken };
Expand Down
6 changes: 3 additions & 3 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { validateToken } = require('./controllers/middlewares/Auth');
const express = require('express');
const cors = require('cors');
const database = require('./models/schemas');
const userRoute = require('./views/routes/Users');
const electiveRoute = require('./views/routes/Electives')
Expand All @@ -9,9 +9,9 @@ require("dotenv").config();
const app = express();
const port = 3001;
app.use(express.json());
app.use(validateToken);
app.use(cors());

app.use(userRoute);
app.use('/auth', userRoute);
app.use('/elective', electiveRoute);
app.use('/learning_paths', learningPathRoute);

Expand Down
2 changes: 1 addition & 1 deletion backend/views/routes/Electives.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const router = express.Router();
const electivesController = require('../../controllers/ElectiveControllers')

router.post("/createElective", electivesController.createElective);
router.delete("/", electivesController.deleteElective)
router.delete("/deleteElective", electivesController.deleteElective)

module.exports = router;
2 changes: 1 addition & 1 deletion backend/views/routes/LearningPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const router = express.Router();
const learningPathsController = require('../../controllers/LearningPathsController')

router.post("/createLearningPaths", learningPathsController.createLearningPaths);
router.delete("/", learningPathsController.deleteLearningPaths)
router.delete("/deleteLearningPaths", learningPathsController.deleteLearningPaths)

module.exports = router;
9 changes: 5 additions & 4 deletions backend/views/routes/Users.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const express = require('express');
const router = express.Router();
const cookieParser = require('cookie-parser');
const userController = require('../../controllers/UserControllers');
const userController = require('../../controllers/UserControllers');
const { validateToken } = require('../../controllers/middlewares/Auth');

router.use(cookieParser());
router.post('/', userController.userRegister);
router.post('/login', userController.userLogin);

router.get('/profile', validateToken, (req, res) => {
res.json('profile');
});

module.exports = router;
router.post('/register', userController.userRegister);
router.post('/login', userController.userLogin);

module.exports = router;
59 changes: 43 additions & 16 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"axios": "^1.6.2",
"framer-motion": "^10.16.4",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand Down
Binary file added frontend/src/icon/definicoes 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/src/icon/interrogatorio 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/src/icon/sair-alt 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/src/icon/usuario-do-circulo 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1e37d7f

Please sign in to comment.