mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-07 14:35:26 +02:00
added comments to dbinsert script, moved salt and passHash to singe db doc
This commit is contained in:
parent
a45d8752ef
commit
119e88583b
3 changed files with 14 additions and 8 deletions
17
dbinsert.js
17
dbinsert.js
|
@ -4,32 +4,37 @@ const program = require('commander');
|
|||
const bcrypt = require('bcrypt');
|
||||
const saltRounds = 10;
|
||||
|
||||
// TODO: add JSDoc (make a research about external modules)
|
||||
/**
|
||||
* Script for creating user with given password, that will be used to create and edit pages in CodeX.Docs.
|
||||
* Hashes password with bcrypt and inserts it in database.
|
||||
* This script should be used once.
|
||||
* If you want to change password, you must remove old password from database.
|
||||
* Also after setting password you must add it to .env file in PASSHASH property.
|
||||
* @see {https://github.com/tj/commander.js | CommanderJS}
|
||||
*/
|
||||
program
|
||||
.description('Application for inserting new passwords to database.')
|
||||
.usage('insert [password]')
|
||||
.command('insert <password>')
|
||||
.action(async function (password) {
|
||||
let userDoc = null;
|
||||
let saltDoc = null;
|
||||
|
||||
bcrypt.genSalt(saltRounds, function (err1, salt) {
|
||||
if (err1) {
|
||||
return ('Salt generation error');
|
||||
}
|
||||
|
||||
saltDoc = { type: 'salt', saltValue: salt };
|
||||
|
||||
bcrypt.hash(password, salt, async (err2, hash) => {
|
||||
if (err2) {
|
||||
return ('Hash generation error');
|
||||
}
|
||||
|
||||
userDoc = { passHash: hash };
|
||||
userDoc = { passHash: hash, salt: salt };
|
||||
|
||||
await db.insert(saltDoc);
|
||||
await db.insert(userDoc);
|
||||
|
||||
console.log('Generated salt:', saltDoc.saltValue);
|
||||
console.log('Generated salt:', userDoc.salt);
|
||||
console.log('Password hash was inserted as:', userDoc.passHash);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -14,7 +14,7 @@ class User {
|
|||
* @returns {Promise<User>}
|
||||
*/
|
||||
static async get(passHash) {
|
||||
const data = await db.findOne({ passHash: passHash });
|
||||
const data = await db.findOne({ passHash });
|
||||
|
||||
if (!data) {
|
||||
return null;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Pages = require('../controllers/pages');
|
||||
|
||||
const verifyToken = require('./middlewares/token');
|
||||
const allowEdit = require('./middlewares/locals');
|
||||
|
||||
|
@ -48,7 +49,7 @@ router.get('/page/:id', verifyToken, async (req, res, next) => {
|
|||
let pageParent = await page.parent;
|
||||
|
||||
res.render('pages/page', {
|
||||
page, pageParent, isAuthorized: res.locals.isAuthorized
|
||||
page, pageParent
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(404);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue