Delete useless request file in ctm and order Auth controller by function s name

This commit is contained in:
cyril lopez 2017-12-07 15:21:54 +01:00
parent cbc0cf73dd
commit cba9cac629
2 changed files with 46 additions and 124 deletions

View File

@ -1,76 +0,0 @@
import 'whatwg-fetch';
/**
* Parses the JSON returned by a network request
*
* @param {object} response A response from a network request
*
* @return {object} The parsed JSON from the request
*/
function parseJSON(response) {
return response.json();
}
/**
* Checks if a network request came back fine, and throws an error if not
*
* @param {object} response A response from a network request
*
* @return {object|undefined} Returns either the response, or throws an error
*/
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
return parseJSON(response).then(responseFormatted => {
const error = new Error(response.statusText);
error.response = response;
error.response.payload = responseFormatted;
throw error;
});
}
/**
* Format query params
*
* @param params
* @returns {string}
*/
function formatQueryParams(params) {
return Object.keys(params)
.map(k => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
.join('&');
}
/**
* Requests a URL, returning a promise
*
* @param {string} url The URL we want to request
* @param {object} [options] The options we want to pass to "fetch"
*
* @return {object} The response data
*/
export default function request(url, options = {}) {
// Set headers
options.headers = {
'Content-Type': 'application/json',
};
// Add parameters to url
url = _.startsWith(url, '/')
? `${strapi.backendURL}${url}`
: url;
if (options && options.params) {
const params = formatQueryParams(options.params);
url = `${url}?${params}`;
}
// Stringify body object
if (options && options.body) {
options.body = JSON.stringify(options.body);
}
return fetch(url, options).then(checkStatus).then(parseJSON);
}

View File

@ -68,48 +68,32 @@ module.exports = {
}
},
register: async (ctx) => {
const params = _.assign(ctx.request.body, {
provider: 'local'
});
changePassword: async (ctx) => {
const params = _.assign({}, ctx.request.body, ctx.params);
// Password is required.
if (!params.password) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.provide' }] }] : 'Please provide your password.');
}
if (params.password && params.passwordConfirmation && params.password === params.passwordConfirmation && params.code) {
const user = await strapi.query('user', 'users-permissions').findOne({ resetPasswordToken: params.code });
// Throw an error if the password selected by the user
// contains more than two times the symbol '$'.
if (strapi.plugins['users-permissions'].services.user.isHashed(params.password)) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.format' }] }] : 'Your password can not contain more than three times the symbol `$`.');
}
if (!user) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.code.provide' }] }] : 'Incorrect code provided.');
}
// First, check if the user is the first one to register as admin.
const adminUsers = await strapi.query('user', 'users-permissions').find(strapi.utils.models.convertParams('user', { role: '0' }));
// Delete the current code
user.resetPasswordToken = null;
// Check if the user is the first to register
if (adminUsers.length === 0) {
params.role = '0';
} else {
params.role = '1';
}
user.password = await strapi.plugins['users-permissions'].services.user.hashPassword(params);
params.password = await strapi.plugins['users-permissions'].services.user.hashPassword(params);
try {
const user = await strapi.query('user', 'users-permissions').create(params);
// Update the user.
await strapi.query('user', 'users-permissions').update(user);
ctx.send({
jwt: strapi.plugins['users-permissions'].services.jwt.issue(user),
user: _.omit(user.toJSON ? user.toJSON() : user, ['password', 'resetPasswordToken'])
});
} catch(err) {
console.log(err);
const adminError = _.includes(err.message, 'username') ? 'Auth.form.error.username.taken' : 'Auth.form.error.email.taken';
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: adminError }] }] : err.message);
} else if (params.password && params.passwordConfirmation && params.password !== params.passwordConfirmation) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.matching' }] }] : 'Passwords do not match.');
} else {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.params.provide' }] }] : 'Incorrect params provided.');
}
},
@ -159,32 +143,46 @@ module.exports = {
ctx.send({ ok: true });
},
changePassword: async (ctx) => {
const params = _.assign({}, ctx.request.body, ctx.params);
register: async (ctx) => {
const params = _.assign(ctx.request.body, {
provider: 'local'
});
if (params.password && params.passwordConfirmation && params.password === params.passwordConfirmation && params.code) {
const user = await strapi.query('user', 'users-permissions').findOne({ resetPasswordToken: params.code });
// Password is required.
if (!params.password) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.provide' }] }] : 'Please provide your password.');
}
if (!user) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.code.provide' }] }] : 'Incorrect code provided.');
}
// Throw an error if the password selected by the user
// contains more than two times the symbol '$'.
if (strapi.plugins['users-permissions'].services.user.isHashed(params.password)) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.format' }] }] : 'Your password cannot contain more than three times the symbol `$`.');
}
// Delete the current code
user.resetPasswordToken = null;
// First, check if the user is the first one to register as admin.
const adminUsers = await strapi.query('user', 'users-permissions').find(strapi.utils.models.convertParams('user', { role: '0' }));
user.password = await strapi.plugins['users-permissions'].services.user.hashPassword(params);
// Check if the user is the first to register
if (adminUsers.length === 0) {
params.role = '0';
} else {
params.role = '1';
}
// Update the user.
await strapi.query('user', 'users-permissions').update(user);
params.password = await strapi.plugins['users-permissions'].services.user.hashPassword(params);
try {
const user = await strapi.query('user', 'users-permissions').create(params);
ctx.send({
jwt: strapi.plugins['users-permissions'].services.jwt.issue(user),
user: _.omit(user.toJSON ? user.toJSON() : user, ['password', 'resetPasswordToken'])
});
} else if (params.password && params.passwordConfirmation && params.password !== params.passwordConfirmation) {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.password.matching' }] }] : 'Passwords do not match.');
} else {
return ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: 'Auth.form.error.params.provide' }] }] : 'Incorrect params provided.');
} catch(err) {
const adminError = _.includes(err.message, 'username') ? 'Auth.form.error.username.taken' : 'Auth.form.error.email.taken';
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: adminError }] }] : err.message);
}
}
};