6

I am using strapi to create APIs. I want to implement my own Registration API and Login API. I checked the documentation of strapi but i am not finding any custom API for this.

can any one help me on this?

1
  • Which kind of function or field do you want to implement into the custom registration? Commented Aug 24, 2020 at 8:35

3 Answers 3

15

Same answer, but in more detail:

Strapi creates an Auth controller automatically for you and you can overwrite its behavior.

Copy the function(s) you need (e.g. register) from this file:

node_modules/strapi-plugin-users-permissions/controllers/Auth.js

to:

your_project_root/extensions/users-permissions/controllers/Auth.js

Now you can overwrite the behavior, e.g. pass a custom field inside the registration process {"myCustomField": "hello world"} and log it to the console:

async register(ctx) {
  ...
  ...
  // log the custom field
  console.log(params.myCustomField)

  // do something with it, e.g. check whether the value already exists
  // in another content type
  const itExists = await strapi.query('some-content-type').findOne({
    fieldName: params.myCustomField
  });
  if (!itExists) {
    return ctx.badRequest(...)
  } else {
    console.log('check success')
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The new directory should also be users-permissions instead of user-permissions.
In Strapi V4 node_modules/@strapi/plugin-users-permissions/server/controllers/Auth.js
3

Actually, strapi creates an Auth controller to handle these requests. You can just change them to fit in your need.

The path to the controller is:

plugins/users-permissions/controllers/Auth.js

1 Comment

This is the correct approach. Mind that all default plugins are moved to node_modules after Strapi 3.0.0-beta, so take a look into: strapi.io/documentation/3.0.0-beta.x/migration-guide/… for modifying stock plugins.
0

in order to create custom users-permissons apis on server side you have to create

src/extensions/users-permissions/strapi-server.js

and in that file can write or override existing user-permissions plugin apis

here is the example for users/me

const _ = require('lodash');

module.exports = (plugin) => {
  const getController = name => {
    return strapi.plugins['users-permissions'].controller(name);
  };

  // Create the new controller
  plugin.controllers.user.me = async (ctx) => {
    const user = ctx.state.user;

    // User has to be logged in to update themselves
    if (!user) {
      return ctx.unauthorized();
    }
      console.log('calling about meeeeeeeeeee------')
      return;
 
  };

  // Add the custom route
  plugin.routes['content-api'].routes.unshift({
    method: 'GET',
    path: '/users/me',
    handler: 'user.me',
    config: {
        prefix: '',
    }
  });

  return plugin;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.