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
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 13:45:58 +02:00
|
|
|
|
const resetEmailTemplate = url => `
|
|
|
|
|
<p>We heard that you lost your password. Sorry about that!</p>
|
|
|
|
|
|
|
|
|
|
<p>But don’t worry! You can use the following link to reset your password:</p>
|
|
|
|
|
|
|
|
|
|
<p>${url}</p>
|
|
|
|
|
|
|
|
|
|
<p>Thanks.</p>`;
|
|
|
|
|
|
2020-05-22 16:01:34 +02:00
|
|
|
|
/**
|
|
|
|
|
* Send an email to the user if it exists or do nothing
|
|
|
|
|
* @param {Object} param params
|
|
|
|
|
* @param {string} param.email user email for which to reset the password
|
|
|
|
|
*/
|
|
|
|
|
const forgotPassword = async ({ email } = {}) => {
|
|
|
|
|
const user = await strapi.query('user', 'admin').findOne({ email, isActive: true });
|
2020-05-22 13:45:58 +02:00
|
|
|
|
|
2020-05-22 16:01:34 +02:00
|
|
|
|
if (!user) {
|
2020-05-22 13:45:58 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const resetPasswordToken = strapi.admin.services.token.createToken();
|
2020-05-22 16:01:34 +02:00
|
|
|
|
await strapi.admin.services.user.update({ id: user.id }, { resetPasswordToken });
|
2020-05-22 13:45:58 +02:00
|
|
|
|
|
2020-05-28 09:31:52 +02:00
|
|
|
|
const url = `${strapi.config.admin.url}/auth/reset-password?code=${resetPasswordToken}`;
|
2020-05-22 13:45:58 +02:00
|
|
|
|
const body = resetEmailTemplate(url);
|
|
|
|
|
|
|
|
|
|
// Send an email to the admin.
|
2020-05-22 16:01:34 +02:00
|
|
|
|
return strapi.plugins['email'].services.email
|
|
|
|
|
.send({
|
|
|
|
|
to: user.email,
|
|
|
|
|
subject: 'Reset password',
|
|
|
|
|
text: body,
|
|
|
|
|
html: body,
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
// log error server side but do not disclose it to the user to avoid leaking informations
|
|
|
|
|
strapi.log.error(err);
|
|
|
|
|
});
|
2020-05-22 13:45:58 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 16:01:34 +02:00
|
|
|
|
/**
|
|
|
|
|
* Reset a user password
|
|
|
|
|
* @param {Object} param params
|
|
|
|
|
* @param {string} param.resetPasswordToken token generated to request a password reset
|
|
|
|
|
* @param {string} param.password new user password
|
|
|
|
|
*/
|
|
|
|
|
const resetPassword = async ({ resetPasswordToken, password } = {}) => {
|
2020-05-22 13:58:58 +02:00
|
|
|
|
const matchingUser = await strapi
|
|
|
|
|
.query('user', 'admin')
|
|
|
|
|
.findOne({ resetPasswordToken, isActive: true });
|
|
|
|
|
|
|
|
|
|
if (!matchingUser) {
|
2020-05-22 16:01:34 +02:00
|
|
|
|
throw strapi.errors.badRequest();
|
2020-05-22 13:58:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strapi.admin.services.user.update(
|
|
|
|
|
{ id: matchingUser.id },
|
|
|
|
|
{
|
2020-05-22 16:01:34 +02:00
|
|
|
|
password,
|
2020-05-22 13:58:58 +02:00
|
|
|
|
resetPasswordToken: null,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
2020-05-22 13:45:58 +02:00
|
|
|
|
forgotPassword,
|
2020-05-22 13:58:58 +02:00
|
|
|
|
resetPassword,
|
2019-05-24 14:05:25 +02:00
|
|
|
|
};
|