How to extend what Registration does?

What would be the best (and most secure) way to extend what the registration auth/local/register is doing? Should I overwrite the route or is there any other way to extend it?

I want to pass additional data (other than username, email, password) while registering a user, that I want to put in a different Content Type.

You will want to read through the extensions documentation on customizing a plugin.

And in this case it will most likely be in the controllers or the policy.

To extend these files you would create a local folder structure (part of it should already exist) of:

/extensions/users-permissions/controllers/somefile.js
/extensions/users-permissions/config/policies/somefile.js

And mirror over the contents of the file from the source, note that you will need to keep these files in sync manually for every Strapi update as they normally come from the node_modules

We were able to extend the registration function (in our case, to add “name” as part of registration) by creating said files but we didn’t mirror over the entire contents, we simply added our own custom piece.
Is this an issue?
Will this still need to be manually updated for every Strapi update?
For example, we created a file:
./extensions/users-permissions/config/schema.graphql.js
Then just added the contents below. This produced the desired outcome of allowing us to pass in ‘name’ with register & haven’t had issues yet, but haven’t upgraded Strapi since we did this (we only did it a couple of days ago)…

const _ = require('lodash');
​
/**
 * Throws an ApolloError if context body contains a bad request
 * @param contextBody - body of the context object given to the resolver
 * @throws ApolloError if the body is a bad request
 */
function checkBadRequest(contextBody) {
  if (_.get(contextBody, 'statusCode', 200) !== 200) {
    const message = _.get(contextBody, 'error', 'Bad Request');
    const exception = new Error(message);
    exception.code = _.get(contextBody, 'statusCode', 400);
    exception.data = contextBody;
    throw exception;
  }
}
​
module.exports = {
  definition: /* GraphQL */ `
    input CustomUsersPermissionsRegisterInput {
      username: String!
      email: String!
      password: String!
      name: String!
    }
  `,
  mutation: `
    customRegister(input: CustomUsersPermissionsRegisterInput!): UsersPermissionsLoginPayload!
  `,
  resolver: {
    Mutation: {
      customRegister: {
        description: 'Register a user, but allow for additional User fields.',
        resolverOf: 'plugins::users-permissions.auth.register',
        resolver: async (obj, options, { context }) => {
          context.request.body = _.toPlainObject(options.input);
​
          await strapi.plugins['users-permissions'].controllers.auth.register(context);
          let output = context.body.toJSON ? context.body.toJSON() : context.body;
​
          checkBadRequest(output);
          return {
            user: output.user || output,
            jwt: output.jwt,
          };
        },
      },
      },
    },
};

Maybe I’m a bit late to the party, but we have achieved this by simply extending the existing generated GraphQL input. Here is how our schema.graphql.js looks like:

module.exports = {
  definition: `
        extend input UsersPermissionsRegisterInput {
            fullName: String!
        }
    `,
  type: {},
  resolver: {},
};

Strapi propagates all extended field to User creation, so now we can create users with fullName

In the new strapi 4.0, I dont think we can use this anymore…I am trying to put it in /src/index.js…
register({ strapi }) {
const extensionService = strapi.plugin(‘graphql’).service(‘extension’);
extensionService.use(({ nexus }) => ({
types: [
nexus.extendType({
type: ‘UsersPermissionsRegisterInput’,
definition(t) {
// here define fields you need
t.string(‘AndyLai’);
},
}),
]
}));
},

but it doesn’t work…Any help will be appreciated.

with v4, you can use extendInputType instead of extendType

register({ strapi }) {
  const extensionService = strapi.plugin(‘graphql’).service(‘extension’);
  extensionService.use(({ nexus }) => ({
  types: [
  nexus.extendInputType({
    type: ‘UsersPermissionsRegisterInput’,
    definition(t) {
      // here define fields you need
      t.string(‘AndyLai’);
   },
}),
]
}));
},


4 Likes

Thank you friends for your help!
Today I spent the whole day trying to set up registration for Front Next via Strapi. It was necessary to add two fields - First name and Last Name. Until I came across this page, I was in despair.

But everything was solved super!

Here is my code where I combined UsersPermissionsMe and UsersPermissionsRegisterInput.

  register({ strapi }) {
    const extensionService = strapi.plugin('graphql').service('extension');
    extensionService.use(({ nexus }) => ({
      types: [
        nexus.extendType({
          type: 'UsersPermissionsMe',
          definition(t) {
            // Здесь определите поля, которые вам нужны для типа UsersPermissionsMe
            t.string('firstName');
            t.string('lastName');
          },
        }),
        nexus.extendInputType({
          type: 'UsersPermissionsRegisterInput',
          definition(t) {
            // Здесь определите поля, которые вам нужны для типа UsersPermissionsRegisterInput
            t.string('firstName');
            t.string('lastName');
          },
        }),
      ],
    }));
  },