Skip to content

Commit

Permalink
Frontend: risolti problemi hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
rikirandon committed May 30, 2024
1 parent 2efde3e commit 151b0e7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
11 changes: 2 additions & 9 deletions frontend/webapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ app.use(express.static(staticDir));
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');

// app.use(multer)



// Routes
app.use('/utente', userRoutes);
Expand All @@ -56,16 +53,12 @@ app.use(tokenChecker);
// Route for the login pag
app.get('/', (req, res) => {
if (verifyToken(req.cookies)) {
res.redirect('/segnalazioni');
res.redirect('/segnalazioni', { currentPage: 'segnalazioni', isSupportoTecnico: req.cookies.supporto_tecnico });
} else {
res.render('login', { currentPage: 'login' });
res.render('login', { currentPage: 'login', isSupportoTecnico: req.cookies.supporto_tecnico });
}
});

// Route segnalazioni
app.get('/segnalazioni', (req, res) => {
res.render('segnalazioni', { currentPage: 'segnalazioni' });
});

// Route register web user
app.get('/registrazione', (req, res) => {
Expand Down
4 changes: 3 additions & 1 deletion frontend/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
"nodemon": "^3.1.0"
},
"dependencies": {
"bcrypt": "^5.1.1",
"argon2": "^0.40.3",
"axios": "^1.7.2",
"bcrypt": "^5.1.1",
"bootstrap": "^5.3.3",
"cookie-parser": "^1.4.6",
"cookieparser": "^0.1.0",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"express": "^4.19.2",
"js-sha512": "^0.9.0",
"jsonwebtoken": "^9.0.2",
"method-override": "^3.0.0",
"morgan": "^1.10.0",
Expand Down
15 changes: 9 additions & 6 deletions frontend/webapp/routes/aziende.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const express = require('express');
const path = require('path');
const bcrypt = require('bcrypt');

require('dotenv').config();
const {sha512} = require('js-sha512');

const router = express.Router();

Expand All @@ -19,7 +17,12 @@ router.get('/', async (req, res) => {
});

router.post("/", async (req, res) => {
let hashed_password = await bcrypt.hash(req.body.password, 10);
let hashed_password = '';

if (req.body.password) {
hashed_password = sha512.hmac("", req.body.password);
}

fetch(path.join(baseUrl, "/api/v1/aziende"), {
method: "POST",
headers: { "x-access-token": req.cookies.token, "Content-Type": "application/json" },
Expand Down Expand Up @@ -47,11 +50,11 @@ router.post("/", async (req, res) => {
});

router.put("/:id", async (req, res) => {

let hashed_password = '';

if (req.body.password) {
hashed_password = await bcrypt.hash(req.body.password, 10);
hashed_password = sha512.hmac("", req.body.password);
}

fetch(path.join(baseUrl, "/api/v1/aziende/" + req.params.id), {
Expand Down
6 changes: 3 additions & 3 deletions frontend/webapp/routes/segnalazioni.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ router.get("/", async (req, res) => {
}
}));

res.render('segnalazioni', { currentPage: 'segnalazioni', segnalazioni: segnalazioniData.segnalazioni });
res.render('segnalazioni', { currentPage: 'segnalazioni', segnalazioni: segnalazioniData.segnalazioni, isSupportoTecnico: req.cookies.supporto_tecnico });
} catch (error) {
console.error("Errore generale:", error);
res.status(500).send("Errore generale nella gestione delle segnalazioni");
Expand All @@ -81,11 +81,11 @@ router.put("/:id", (req, res) => {
.then(response => {
if (response.ok) {
console.log("PUT request successful. Redirecting to /segnalazioni");
res.status(303).redirect("/segnalazioni");
res.redirect("/segnalazioni");
}
else {
console.error("PUT request failed. Redirecting to /segnalazioni with error.");
res.status(303).redirect("/segnalazioni?error=Qualcosa%20C3%A%20andato%20storto");
res.redirect("/segnalazioni?error=Qualcosa%20C3%A%20andato%20storto");
}
})
.catch(jsonError => {
Expand Down
42 changes: 35 additions & 7 deletions frontend/webapp/routes/user_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const express = require('express');
const path = require('path');
const jwt = require('jsonwebtoken');
const {sha512} = require('js-sha512');
require('dotenv').config();

// Create router
Expand All @@ -13,21 +14,36 @@ const baseUrl = process.env.BACKEND_BASE_URL || "http://localhost:8080";
router.post('/', async (req, res) => {
// Make API request
console.log(req.body);
// set supporto tecnico as boolean
req.body.supporto_tecnico = req.body.supporto_tecnico==='true';

let hashedPassword = "";
if (req.body.password) {
hashedPassword = sha512.hmac("", req.body.password);
}

console.log({
nome: req.body.nome,
email: req.body.email,
password: hashedPassword,
supporto_tecnico: req.body.supporto_tecnico === 'true'
});
fetch(path.join(baseUrl, '/api/v1/utente/web'), {
method: "POST",
headers: { "Content-Type": "application/json", "x-access-token": req.cookies.token },
body: JSON.stringify(req.body)
body: JSON.stringify({
nome: req.body.nome,
email: req.body.email,
password: hashedPassword,
supporto_tecnico: req.body.supporto_tecnico === 'true'
})
})
.then(response => {
response.json()
.then(body => {
if (response.ok) {
res.render('registrazione', { successMessage: "Utente regitrato correttamente", currentPage: 'login' });
res.render('registrazione', { successMessage: "Utente regitrato correttamente", currentPage: 'registrazione', isSupportoTecnico: req.cookies.supporto_tecnico });
}
else {
res.render('registrazione', { errorMessage: body.error, currentPage: 'registrazione' });
res.render('registrazione', { errorMessage: body.error, currentPage: 'registrazione', isSupportoTecnico: req.cookies.supporto_tecnico });
}
})
.catch(jsonError => {
Expand All @@ -41,12 +57,24 @@ router.post('/', async (req, res) => {

// Route for user login
router.post('/login', async (req, res) => {
console.log("og pass: " + req.body.password);
let hashedPassword = "";
if (req.body.password) {
hashedPassword = sha512.hmac("", req.body.password);
}
// Make API request
//console.log(req.body);
console.log({
email: req.body.email,
password: hashedPassword
});
fetch(path.join(baseUrl, '/api/v1/utente/web/login'), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(req.body)
body: JSON.stringify({
email: req.body.email,
password: hashedPassword,
})
})
.then(response => {
response.json()
Expand All @@ -61,7 +89,7 @@ router.post('/login', async (req, res) => {
} else {
// respone error, send error message to render
console.log(body);
return res.render('login', { errorMessage: body.error, currentPage: 'login' });
return res.render('login', { errorMessage: body.error, currentPage: 'login', isSupportoTecnico: req.cookies.supporto_tecnico });
}

})
Expand Down
2 changes: 1 addition & 1 deletion frontend/webapp/views/segnalazioni.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<option selected disabled>Seleziona lo stato</option>
<option value="aperta">Aperta</option>
<option value="presa_in_carico">Presa in carico</option>
<option value="conclusa">Chiusa</option>
<option value="conclusa">Conclusa</option>
</select>
</div>
<div class="modal-footer">
Expand Down

0 comments on commit 151b0e7

Please sign in to comment.