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>`;
|
|
|
|
|
|
|
|
|
|
const forgotPassword = async ({ email }) => {
|
2020-05-22 13:58:58 +02:00
|
|
|
|
const admin = await strapi.query('user', 'admin').findOne({ email, isActive: true });
|
2020-05-22 13:45:58 +02:00
|
|
|
|
|
|
|
|
|
// admin not found => do nothing
|
|
|
|
|
if (!admin) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate random token.
|
|
|
|
|
const resetPasswordToken = strapi.admin.services.token.createToken();
|
|
|
|
|
|
|
|
|
|
await strapi.admin.services.user.update({ email }, { resetPasswordToken });
|
|
|
|
|
|
|
|
|
|
// TODO: set the final url once the front is developed
|
|
|
|
|
const url = `${strapi.config.admin.url}/reset-password?code=${resetPasswordToken}`;
|
|
|
|
|
|
|
|
|
|
const body = resetEmailTemplate(url);
|
|
|
|
|
|
|
|
|
|
// Send an email to the admin.
|
|
|
|
|
await strapi.plugins['email'].services.email.send({
|
|
|
|
|
to: admin.email,
|
|
|
|
|
subject: 'Reset password',
|
|
|
|
|
text: body,
|
|
|
|
|
html: body,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 13:58:58 +02:00
|
|
|
|
const resetPassword = async ({ resetPasswordToken, password }) => {
|
|
|
|
|
const matchingUser = await strapi
|
|
|
|
|
.query('user', 'admin')
|
|
|
|
|
.findOne({ resetPasswordToken, isActive: true });
|
|
|
|
|
|
|
|
|
|
if (!matchingUser) {
|
|
|
|
|
throw strapi.errors.badRequest('Invalid reset token');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strapi.admin.services.user.update(
|
|
|
|
|
{ id: matchingUser.id },
|
|
|
|
|
{
|
|
|
|
|
password: password,
|
|
|
|
|
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
|
|
|
|
};
|