Beginning of user registration

This commit is contained in:
Ethanell 2020-04-26 16:24:48 +02:00
parent 6cf94537a4
commit a28c293e87
3 changed files with 31 additions and 1 deletions

3
.gitignore vendored
View file

@ -2,3 +2,6 @@
node_modules node_modules
package-lock.json package-lock.json
# Image Node data
users.json

28
app.js
View file

@ -1,7 +1,33 @@
let express = require("express"); let express = require("express");
let morgan = require("morgan") let morgan = require("morgan");
let fs = require("fs");
let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false});
let passwordHash = require('password-hash');
let app = express(); let app = express();
if (!fs.existsSync("users.json")) {
fs.writeFileSync("users.json", "{}");
console.log("Register first user");
addUser();
} else if (Object.keys(JSON.parse(fs.readFileSync("users.json"))).length === 0) {
console.log("No user found, adding a new one");
addUser();
}
async function addUser() {
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));
}
app.use(morgan("dev")) app.use(morgan("dev"))
.use(express.static("public")) .use(express.static("public"))
.set("view engine", "pug") .set("view engine", "pug")

View file

@ -6,6 +6,7 @@
"dependencies": { "dependencies": {
"express": "^4.17.1", "express": "^4.17.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"password-hash": "^1.2.2",
"pug": "^2.0.4" "pug": "^2.0.4"
}, },
"author": "flifloo" "author": "flifloo"