mirror of
https://github.com/strapi/strapi.git
synced 2025-09-28 18:01:26 +00:00
51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
/**
|
|
* hashes a password
|
|
* @param {string} password - password to hash
|
|
* @returns {string} hashed password
|
|
*/
|
|
const hashPassword = password => bcrypt.hash(password, 10);
|
|
|
|
/**
|
|
* Validate a password
|
|
* @param {string} password
|
|
* @param {string} hash
|
|
* @returns {boolean} is the password valid
|
|
*/
|
|
const validatePassword = (password, hash) => bcrypt.compare(password, hash);
|
|
|
|
/**
|
|
* Check login credentials
|
|
* @param {Object} options
|
|
* @param {string} options.email
|
|
* @param {string} options.password
|
|
*/
|
|
const checkCredentials = async ({ email, password }) => {
|
|
const user = await strapi.query('user', 'admin').findOne({ email });
|
|
|
|
if (!user) {
|
|
return [null, false, { message: 'Invalid credentials' }];
|
|
}
|
|
|
|
const isValid = await validatePassword(password, user.password);
|
|
|
|
if (!isValid) {
|
|
return [null, false, { message: 'Invalid credentials' }];
|
|
}
|
|
|
|
if (!(user.isActive === true)) {
|
|
return [null, false, { message: 'User not active' }];
|
|
}
|
|
|
|
return [null, user];
|
|
};
|
|
|
|
module.exports = {
|
|
checkCredentials,
|
|
validatePassword,
|
|
hashPassword,
|
|
};
|