You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/user/password.js

40 lines
982 B
JavaScript

10 years ago
'use strict';
const nconf = require('nconf');
const db = require('../database');
const Password = require('../password');
10 years ago
module.exports = function (User) {
User.hashPassword = async function (password) {
10 years ago
if (!password) {
return password;
10 years ago
}
return await Password.hash(nconf.get('bcrypt_rounds') || 12, password);
10 years ago
};
User.isPasswordCorrect = async function (uid, password, ip) {
9 years ago
password = password || '';
var hashedPassword = await db.getObjectField('user:' + uid, 'password');
if (!hashedPassword) {
// Non-existant user, submit fake hash for comparison
hashedPassword = '';
}
await User.isPasswordValid(password, 0);
await User.auth.logAttempt(uid, ip);
const ok = await Password.compare(password, hashedPassword);
if (ok) {
User.auth.clearLoginAttempts(uid);
}
return ok;
10 years ago
};
User.hasPassword = async function (uid) {
const hashedPassword = await db.getObjectField('user:' + uid, 'password');
return !!hashedPassword;
};
8 years ago
};