diff --git a/api/extensions/users-permissions/config/routes.json b/api/extensions/users-permissions/config/routes.json index f038d6d..d035a69 100644 --- a/api/extensions/users-permissions/config/routes.json +++ b/api/extensions/users-permissions/config/routes.json @@ -29,21 +29,6 @@ "actionType": "update" } } - }, - { - "method": "POST", - "path": "/auth/local/register", - "handler": "User.create", - "config": { - "policies": [], - "prefix": "", - "description": "Create a user in Strapi and the AstroPlant Core API", - "tag": { - "plugin": "users-permissions", - "name": "User", - "actionType": "create" - } - } } ] } \ No newline at end of file diff --git a/api/extensions/users-permissions/controllers/Auth.js b/api/extensions/users-permissions/controllers/Auth.js index 97735de..04996a0 100644 --- a/api/extensions/users-permissions/controllers/Auth.js +++ b/api/extensions/users-permissions/controllers/Auth.js @@ -13,12 +13,102 @@ const grant = require("grant-koa"); const { sanitizeEntity } = require("strapi-utils"); +const sanitizeUser = user => + sanitizeEntity(user, { + model: strapi.query("user", "users-permissions").model + }); + 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,}))$/; const formatError = error => [ { messages: [{ id: error.id, message: error.message, field: error.field }] } ]; module.exports = { + + /** + * Registers the user in Strapi and the authenticates with the AstroPlant Core API. + */ + async register(ctx) { + const advanced = await strapi + .store({ + environment: "", + type: "plugin", + name: "users-permissions", + key: "advanced" + }) + .get(); + + const { email, username, password } = ctx.request.body; + + if (!email) return ctx.badRequest("missing.email"); + if (!username) return ctx.badRequest("missing.username"); + if (!password) return ctx.badRequest("missing.password"); + + const userWithSameUsername = await strapi + .query("user", "users-permissions") + .findOne({ username }); + + if (userWithSameUsername) { + return ctx.badRequest( + null, + formatError({ + id: "Auth.form.error.username.taken", + message: "Username already taken.", + field: ["username"] + }) + ); + } + + if (advanced.unique_email) { + const userWithSameEmail = await strapi + .query("user", "users-permissions") + .findOne({ email }); + + if (userWithSameEmail) { + return ctx.badRequest( + null, + + formatError({ + id: "Auth.form.error.email.taken", + message: "Email already taken.", + field: ["email"] + }) + ); + } + } + + const user = { + ...ctx.request.body, + provider: "local" + }; + + const defaultRole = await strapi + .query("role", "users-permissions") + .findOne({ type: advanced.default_role }, []); + + user.role = defaultRole.id; + + try { + // Try to create the user on the data api if it doesn't already exist + await strapi.config.functions.astroplant.signup(ctx, { + username: username, + email: email, + password: password + }); + + const data = await strapi.plugins["users-permissions"].services.user.add( + user + ); + + ctx.created(sanitizeUser(data)); + } catch (error) { + ctx.badRequest(null, formatError(error)); + } + }, + + /** + * Handles the /auth/local request + */ async callback(ctx) { const provider = ctx.params.provider || "local"; const params = ctx.request.body; diff --git a/api/extensions/users-permissions/controllers/User.js b/api/extensions/users-permissions/controllers/User.js index 4609e42..d55040a 100644 --- a/api/extensions/users-permissions/controllers/User.js +++ b/api/extensions/users-permissions/controllers/User.js @@ -24,7 +24,6 @@ module.exports = { * @return {Object} */ async create(ctx) { - console.log("CREATING USER") const advanced = await strapi .store({ environment: "", @@ -87,7 +86,6 @@ module.exports = { } try { - console.log("CRETATING USER", username) // Try to create the user on the data api if it doesn't already exist await strapi.config.functions.astroplant.signup(ctx, { username: username,