2020-04-26 20:12:50 +02:00
|
|
|
let fs = require("fs");
|
|
|
|
let passwordHash = require('password-hash');
|
|
|
|
|
|
|
|
|
|
|
|
async function addUser() {
|
|
|
|
let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false});
|
|
|
|
let username;
|
|
|
|
let password;
|
|
|
|
file = JSON.parse(fs.readFileSync("users.json"));
|
|
|
|
do {
|
|
|
|
username = await new Promise(resolve => rl.question("Username: ", resolve));
|
|
|
|
} while (username in file || ["", null].indexOf(username) >= 0);
|
|
|
|
password = passwordHash.generate(await new Promise(resolve => rl.question("Password: ", resolve)));
|
|
|
|
file[username] = password;
|
|
|
|
fs.writeFileSync("users.json", JSON.stringify(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAuth(req, res, next) {
|
|
|
|
if (req.session.login) {
|
|
|
|
next();
|
|
|
|
} else {
|
2020-04-28 16:51:43 +02:00
|
|
|
res.redirect("/login?path=" + req.originalUrl);
|
2020-04-26 20:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.addUser = addUser;
|
|
|
|
module.exports.isAuth = isAuth;
|
|
|
|
|