Created
June 7, 2019 10:11
-
-
Save alexandrebodin/2661ec1ed75be2ca2886e11ff151da51 to your computer and use it in GitHub Desktop.
Auth custom populate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 = { | |
callback: async ctx => { | |
const provider = ctx.params.provider || 'local'; | |
const params = ctx.request.body; | |
const store = await strapi.store({ | |
environment: '', | |
type: 'plugin', | |
name: 'users-permissions', | |
}); | |
if (provider === 'local') { | |
if ( | |
!_.get(await store.get({ key: 'grant' }), 'email.enabled') && | |
!ctx.request.admin | |
) { | |
return ctx.badRequest(null, 'This provider is disabled.'); | |
} | |
// The identifier is required. | |
if (!params.identifier) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.email.provide' }] }] | |
: 'Please provide your username or your e-mail.' | |
); | |
} | |
// The password is required. | |
if (!params.password) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.password.provide' }] }] | |
: 'Please provide your password.' | |
); | |
} | |
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 user exists. | |
const user = await strapi.plugins['users-permissions'] | |
.queries('user', 'users-permissions') | |
.findOne(query, ['role', 'usersetting']); | |
if (!user) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.invalid' }] }] | |
: 'Identifier or password invalid.' | |
); | |
} | |
if ( | |
_.get(await store.get({ key: 'advanced' }), 'email_confirmation') && | |
user.confirmed !== true | |
) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.confirmed' }] }] | |
: 'Your account email is not confirmed.' | |
); | |
} | |
if (user.blocked === true) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.blocked' }] }] | |
: 'Your account has been blocked by the administrator.' | |
); | |
} | |
// The user never authenticated with the `local` provider. | |
if (!user.password) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.password.local' }] }] | |
: 'This user never set a local password, please login thanks to the provider used during account creation.' | |
); | |
} | |
const validPassword = strapi.plugins[ | |
'users-permissions' | |
].services.user.validatePassword(params.password, user.password); | |
if (!validPassword) { | |
return ctx.badRequest( | |
null, | |
ctx.request.admin | |
? [{ messages: [{ id: 'Auth.form.error.invalid' }] }] | |
: 'Identifier or password invalid.' | |
); | |
} else { | |
ctx.send({ | |
jwt: strapi.plugins['users-permissions'].services.jwt.issue( | |
_.pick(user.toJSON ? user.toJSON() : user, ['_id', 'id']) | |
), | |
user: _.omit(user.toJSON ? user.toJSON() : user, [ | |
'password', | |
'resetPasswordToken', | |
]), | |
}); | |
} | |
} else { | |
if (!_.get(await store.get({ key: 'grant' }), [provider, 'enabled'])) { | |
return ctx.badRequest(null, 'This provider is disabled.'); | |
} | |
// Connect the user thanks to the third-party provider. | |
let user, error; | |
try { | |
[user, error] = await strapi.plugins[ | |
'users-permissions' | |
].services.providers.connect(provider, ctx.query); | |
} catch ([user, error]) { | |
return ctx.badRequest( | |
null, | |
error === 'array' ? (ctx.request.admin ? error[0] : error[1]) : error | |
); | |
} | |
if (!user) { | |
return ctx.badRequest( | |
null, | |
error === 'array' ? (ctx.request.admin ? error[0] : error[1]) : error | |
); | |
} | |
ctx.send({ | |
jwt: strapi.plugins['users-permissions'].services.jwt.issue( | |
_.pick(user, ['_id', 'id']) | |
), | |
user: _.omit(user.toJSON ? user.toJSON() : user, [ | |
'password', | |
'resetPasswordToken', | |
]), | |
}); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment