2020-05-11 17:09:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
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);
|
|
|
|
|
2020-05-11 17:09:48 +02:00
|
|
|
/**
|
|
|
|
* Check login credentials
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {string} options.email
|
|
|
|
* @param {string} options.password
|
|
|
|
*/
|
|
|
|
const checkCredentials = async ({ email, password }) => {
|
2020-05-13 11:46:52 +02:00
|
|
|
const user = await strapi.query('user', 'admin').findOne({ email });
|
2020-05-11 17:09:48 +02:00
|
|
|
|
2020-05-14 16:29:50 +02:00
|
|
|
if (!user || !user.password) {
|
2020-05-12 14:57:24 +02:00
|
|
|
return [null, false, { message: 'Invalid credentials' }];
|
2020-05-11 17:09:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 11:46:52 +02:00
|
|
|
const isValid = await validatePassword(password, user.password);
|
2020-05-11 17:09:48 +02:00
|
|
|
|
|
|
|
if (!isValid) {
|
2020-05-12 14:57:24 +02:00
|
|
|
return [null, false, { message: 'Invalid credentials' }];
|
2020-05-11 17:09:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 11:46:52 +02:00
|
|
|
if (!(user.isActive === true)) {
|
2020-05-12 14:57:24 +02:00
|
|
|
return [null, false, { message: 'User not active' }];
|
2020-05-11 17:09:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 11:46:52 +02:00
|
|
|
return [null, user];
|
2020-05-12 13:21:26 +02:00
|
|
|
};
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
module.exports = {
|
2020-05-11 17:09:48 +02:00
|
|
|
checkCredentials,
|
2019-05-24 14:05:25 +02:00
|
|
|
validatePassword,
|
|
|
|
hashPassword,
|
|
|
|
};
|