2019-04-09 12:09:03 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Auth.js controller
|
|
|
|
|
*
|
|
|
|
|
* @description: A set of functions called "actions" for managing `Auth`.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* eslint-disable no-useless-escape */
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
|
|
const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2019-07-15 19:33:07 +02:00
|
|
|
|
async callback(ctx) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
const params = ctx.request.body;
|
|
|
|
|
|
|
|
|
|
// The identifier is required.
|
|
|
|
|
if (!params.identifier) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.email.provide' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Please provide your username or your e-mail.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The password is required.
|
|
|
|
|
if (!params.password) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.password.provide' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Please provide your password.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = {};
|
|
|
|
|
|
|
|
|
|
// Check if the provided identifier is an email or not.
|
|
|
|
|
const isEmail = emailRegExp.test(params.identifier);
|
|
|
|
|
|
|
|
|
|
// Set the identifier to the appropriate query field.
|
|
|
|
|
if (isEmail) {
|
|
|
|
|
query.email = params.identifier.toLowerCase();
|
|
|
|
|
} else {
|
|
|
|
|
query.username = params.identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the admin exists.
|
2019-07-15 19:33:07 +02:00
|
|
|
|
const admin = await strapi.query('administrator', 'admin').findOne(query);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
|
|
if (!admin) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.invalid' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Identifier or password invalid.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (admin.blocked === true) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.blocked' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Your account has been blocked by the administrator.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
const validPassword = await strapi.admin.services.auth.validatePassword(
|
|
|
|
|
params.password,
|
|
|
|
|
admin.password
|
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
|
|
if (!validPassword) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.invalid' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Identifier or password invalid.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
admin.isAdmin = true;
|
|
|
|
|
|
|
|
|
|
ctx.send({
|
2019-05-24 14:05:25 +02:00
|
|
|
|
jwt: strapi.admin.services.auth.createJwtToken(admin),
|
|
|
|
|
user: strapi.admin.services.auth.sanitizeUser(admin),
|
2019-04-09 12:09:03 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
async register(ctx) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
const params = ctx.request.body;
|
|
|
|
|
|
|
|
|
|
// Username is required.
|
|
|
|
|
if (!params.username) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.username.provide' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Please provide your username.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Email is required.
|
|
|
|
|
if (!params.email) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.email.provide' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Please provide your email.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
// Check if the provided email is valid or not.
|
|
|
|
|
const isEmail = emailRegExp.test(params.email);
|
|
|
|
|
|
|
|
|
|
if (isEmail) {
|
|
|
|
|
params.email = params.email.toLowerCase();
|
|
|
|
|
} else {
|
|
|
|
|
return ctx.badRequest('Invalid email format');
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
|
// Password is required.
|
|
|
|
|
if (!params.password) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.password.provide' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Please provide your password.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
// First, check if their is at least one admin
|
2019-07-15 19:33:07 +02:00
|
|
|
|
const admins = await strapi
|
|
|
|
|
.query('administrator', 'admin')
|
2019-05-24 14:05:25 +02:00
|
|
|
|
.find({ _limit: 1 });
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
if (admins.length > 0) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.admin.exist' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: "You can't register a new admin."
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
params.password = await strapi.admin.services.auth.hashPassword(
|
|
|
|
|
params.password
|
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
const admin = await strapi.query('administrator', 'admin').findOne({
|
2019-04-09 12:09:03 +02:00
|
|
|
|
email: params.email,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (admin) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.email.taken' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Email is already taken.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2019-07-15 19:33:07 +02:00
|
|
|
|
const admin = await strapi.query('administrator', 'admin').create(params);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
|
|
admin.isAdmin = true;
|
|
|
|
|
|
2019-05-24 14:05:25 +02:00
|
|
|
|
const jwt = strapi.admin.services.auth.createJwtToken(admin);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
|
|
strapi.emit('didCreateFirstAdmin');
|
|
|
|
|
|
|
|
|
|
ctx.send({
|
|
|
|
|
jwt,
|
2019-05-24 14:05:25 +02:00
|
|
|
|
user: strapi.admin.services.auth.sanitizeUser(admin),
|
2019-04-09 12:09:03 +02:00
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
2019-04-16 09:35:13 +02:00
|
|
|
|
strapi.log.error(err);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
const adminError = _.includes(err.message, 'username')
|
|
|
|
|
? 'Auth.form.error.username.taken'
|
|
|
|
|
: 'Auth.form.error.email.taken';
|
|
|
|
|
|
|
|
|
|
ctx.badRequest(
|
|
|
|
|
null,
|
2019-05-24 14:05:25 +02:00
|
|
|
|
ctx.request.admin ? [{ messages: [{ id: adminError }] }] : err.message
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
async changePassword(ctx) {
|
|
|
|
|
const { password, passwordConfirmation, code } = {
|
|
|
|
|
...ctx.request.body,
|
|
|
|
|
...ctx.params,
|
|
|
|
|
};
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
if (!password) return ctx.badRequest('Missing password');
|
|
|
|
|
if (!passwordConfirmation)
|
|
|
|
|
return ctx.badRequest('Missing passwordConfirmation');
|
|
|
|
|
if (!code) return ctx.badRequest('Missing code');
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
if (password !== passwordConfirmation) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.password.matching' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'Passwords do not match.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
2019-07-15 19:33:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const admin = await strapi
|
|
|
|
|
.query('administrator', 'admin')
|
|
|
|
|
.findOne({ resetPasswordToken: code });
|
|
|
|
|
|
|
|
|
|
if (!admin) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
2019-07-15 19:33:07 +02:00
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.code.provide' }] }]
|
|
|
|
|
: 'Incorrect code provided.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
2019-07-15 19:33:07 +02:00
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
resetPasswordToken: null,
|
|
|
|
|
password: await strapi.admin.services.auth.hashPassword(password),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updatedAdmin = await strapi
|
|
|
|
|
.query('administrator', 'admin')
|
|
|
|
|
.update({ id: admin.id }, data);
|
|
|
|
|
|
|
|
|
|
return ctx.send({
|
|
|
|
|
jwt: strapi.admin.services.auth.createJwtToken(updatedAdmin),
|
|
|
|
|
user: strapi.admin.services.auth.sanitizeUser(updatedAdmin),
|
|
|
|
|
});
|
2019-04-09 12:09:03 +02:00
|
|
|
|
},
|
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
async forgotPassword(ctx) {
|
2019-04-09 12:09:03 +02:00
|
|
|
|
const { email, url } = ctx.request.body;
|
|
|
|
|
|
2019-07-15 19:33:07 +02:00
|
|
|
|
if (!email) return ctx.badRequest('Missing email');
|
|
|
|
|
if (!url) return ctx.badRequest('Missing url');
|
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
|
// Find the admin thanks to his email.
|
2019-07-15 19:33:07 +02:00
|
|
|
|
const admin = await strapi
|
|
|
|
|
.query('administrator', 'admin')
|
2019-04-09 12:09:03 +02:00
|
|
|
|
.findOne({ email });
|
|
|
|
|
|
|
|
|
|
// admin not found.
|
|
|
|
|
if (!admin) {
|
|
|
|
|
return ctx.badRequest(
|
|
|
|
|
null,
|
|
|
|
|
ctx.request.admin
|
|
|
|
|
? [{ messages: [{ id: 'Auth.form.error.user.not-exist' }] }]
|
2019-05-24 14:05:25 +02:00
|
|
|
|
: 'This email does not exist.'
|
2019-04-09 12:09:03 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate random token.
|
|
|
|
|
const resetPasswordToken = crypto.randomBytes(64).toString('hex');
|
|
|
|
|
|
|
|
|
|
const settings = {
|
|
|
|
|
from: {
|
|
|
|
|
name: 'Administration Panel',
|
|
|
|
|
email: 'no-reply@strapi.io',
|
|
|
|
|
},
|
|
|
|
|
response_email: '',
|
|
|
|
|
object: 'Reset password',
|
|
|
|
|
message: `<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}?code=${resetPasswordToken}</p>
|
|
|
|
|
|
|
|
|
|
<p>Thanks.</p>`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Send an email to the admin.
|
|
|
|
|
await strapi.plugins['email'].services.email.send({
|
|
|
|
|
to: admin.email,
|
|
|
|
|
from:
|
|
|
|
|
settings.from.email || settings.from.name
|
|
|
|
|
? `"${settings.from.name}" <${settings.from.email}>`
|
|
|
|
|
: undefined,
|
|
|
|
|
replyTo: settings.response_email,
|
|
|
|
|
subject: settings.object,
|
|
|
|
|
text: settings.message,
|
|
|
|
|
html: settings.message,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return ctx.badRequest(null, err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the admin.
|
2019-07-15 19:33:07 +02:00
|
|
|
|
await strapi
|
|
|
|
|
.query('administrator', 'admin')
|
|
|
|
|
.update({ id: admin.id }, { resetPasswordToken });
|
2019-04-09 12:09:03 +02:00
|
|
|
|
|
|
|
|
|
ctx.send({ ok: true });
|
|
|
|
|
},
|
|
|
|
|
};
|