mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.4 KiB
64 lines
1.4 KiB
6 years ago
|
'use strict';
|
||
|
|
||
|
const bcrypt = require('bcrypt')
|
||
|
, Accounts = require(__dirname+'/../../db-models/accounts.js');
|
||
|
|
||
|
module.exports = async (req, res) => {
|
||
|
|
||
|
const username = req.body.username.toLowerCase();
|
||
|
const password = req.body.password;
|
||
|
|
||
|
//fetch an account
|
||
|
let account;
|
||
|
try {
|
||
|
account = await Accounts.findOne(username);
|
||
|
} catch (err) {
|
||
|
console.error(err);
|
||
|
return res.status(500).render('error');
|
||
|
}
|
||
|
|
||
|
//if the account doesnt exist, reject
|
||
|
if (!account) {
|
||
|
return res.status(403).render('message', {
|
||
|
'title': 'Forbidden',
|
||
|
'message': 'Incorrect username or password',
|
||
|
'redirect': '/login'
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// bcrypt compare input to saved hash
|
||
|
let passwordMatch;
|
||
|
try {
|
||
|
passwordMatch = await bcrypt.compare(password, account.passwordHash);
|
||
|
} catch (err) {
|
||
|
console.error(err);
|
||
|
return res.status(500).render('error');
|
||
|
}
|
||
|
|
||
|
//if hashes matched
|
||
|
if (passwordMatch === true) {
|
||
|
|
||
|
// add the account to the session and authenticate if password was correct
|
||
6 years ago
|
req.session.user = {
|
||
|
'username': account._id,
|
||
|
'authLevel': account.authLevel
|
||
|
};
|
||
6 years ago
|
req.session.authenticated = true;
|
||
|
|
||
|
//successful login
|
||
|
return res.render('message', {
|
||
|
'title': 'Success',
|
||
|
'message': `Welcome, ${username}`,
|
||
|
'redirect': '/'
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
return res.status(403).render('message', {
|
||
|
'title': 'Forbidden',
|
||
|
'message': 'Incorrect username or password',
|
||
|
'redirect': '/login'
|
||
|
});
|
||
|
|
||
|
}
|