2017-10-16 04:32:49 +08:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2017-10-16 07:47:05 +08:00
|
|
|
const utils = require('../services/utils');
|
|
|
|
const sql = require('../services/sql');
|
|
|
|
const my_scrypt = require('../services/my_scrypt');
|
2017-10-16 04:32:49 +08:00
|
|
|
|
|
|
|
router.get('', (req, res, next) => {
|
|
|
|
res.render('login', { 'failedAuth': false });
|
|
|
|
});
|
|
|
|
|
|
|
|
router.post('', async (req, res, next) => {
|
|
|
|
const userName = await sql.getOption('username');
|
|
|
|
|
|
|
|
const guessedPassword = req.body.password;
|
|
|
|
|
|
|
|
if (req.body.username === userName && await verifyPassword(guessedPassword)) {
|
|
|
|
const rememberMe = req.body.rememberme;
|
|
|
|
|
|
|
|
req.session.loggedIn = true;
|
|
|
|
|
2017-10-16 07:47:05 +08:00
|
|
|
return res.redirect('/');
|
2017-10-16 04:32:49 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.render('login', {'failedAuth': true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function verifyPassword(guessed_password) {
|
|
|
|
const hashed_password = utils.fromBase64(await sql.getOption('password_verification_hash'));
|
|
|
|
|
|
|
|
const guess_hashed = await my_scrypt.getVerificationHash(guessed_password);
|
|
|
|
|
|
|
|
return guess_hashed.equals(hashed_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router;
|