# `mkpasswd -m bcrypt` for gen password.
[[users]]
username = "fabio"
password = ""
[[users]]
username = "rita"
password = ""
La password si puo' ottenere col comando `mkpasswd -m bcrypt` .
===== Logica =====
Per ogni rotta che si deve proteggere con autenticazione controllo se esiste una variabile di sessione **logged="yes"**. Se non trovo la variabile di sessione allora avviene un redirect alla rotta che porta al form di autenticazione. \\
A sua volta il form ha un'action su **/dologin** che controlla se esiste un utente che ha la password e il relativo nome utente specificati nei campi del form. Se esiste l'utente allora assegno a **logged** il valore **yes**.
===== Le rotte =====
import express from "express";
import * as utils from "./utils.js" ;
const app = express();
const session = require('express-session');
app.use(session({
secret: 'secret_dfhgighd', // required – signs the session ID cookie
resave: false, // don’t save unchanged sessions
saveUninitialized: true, // save new but unmodified sessions
cookie: { // cookie options for the session ID
maxAge: 60000*60*24*10, // 60.000 = 1 minute (ms)
httpOnly: true,
secure: false, // set true for HTTPS (or 'auto')
sameSite: 'lax' // or 'strict', 'none', 'auto'
}
}));
const mount_point="/myapp";
app.get("/login", (req,res) =>{
res.render("login", {mount_point: mount_point});
});
app.post("/dologin", async (req,res) =>{
const user= req.body.username;
const pass= req.body.password;
const ok = await utils.is_valid_user(user, pass);
if (ok){
req.session.logged="yes";
}
res.redirect(mount_point);
});
app.get("/logout", (req,res) =>{
req.session.logged="no";
res.redirect(mount_point);
});
===== Funzioni nel file utils.js =====
**utils.js**
import bcrypt from 'bcrypt';
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import { readFileSync } from "node:fs";
import toml from "toml";
const toml_users=`${__dirname}/users.toml`
export function only_logged(req,res){
if (req.session.logged!="yes"){
return false;
}else{
return true;
}
}
export async function is_valid_user(u,p){
const data = readFileSync(toml_users, "utf8");
const conf = toml.parse(data);
for (const e of conf.users){
// console.log(e.username+" -- "+e.password);
if (e.username==u){
const match_pass = await bcrypt.compare(p, e.password);
if (match_pass){
console.log("Utente corretto.");
return true;
}
}
}
return false;
}
===== Template per il form di login =====
**views/login.ejs**
<%- include('header') %>
Login please...
<%- include('footer') %>
===== Generare password bcrypt =====
Ecco una rotta che genera password bcrypt in questo modo:
...
import bcrypt from "bcrypt";
app.get("/genpassword/:password", async (req, res) => {
const saltRounds = 10;
const password = req.params.password;
const hash = await bcrypt.hash(password, saltRounds);
res.send(hash);
});
...