Extending /auth/local/register endpoint with custom logic

Hi @Konstantin_Cube
I solved my case by creating a custom middleware. To do so do the following:

  1. create a file with a name by your choice in src/middlewares/yourfile.js
  2. In this file: Hook into Account creation with some code in the created file like this:
module.exports = () => {
  return async (ctx, next) => {
    await next();
    // only if path was register with newsletter param and it was successfull. Then we will put user in the mailing list.
    if (ctx.request.url === '/api/auth/local/register?newsletter=true' && ctx.response.status === 200) {
      const email = ctx.response.body.user.email;
      const name = ctx.response.body.user.nickname;
      // we do NOT await this. it has nothing to with account creation and should not block it. just a side effect.
      registerToNewsletter(email, name);
    }
  };
};
  1. Enable your middleware in src/config/middlewares.js
    f.e. like this: (See Reference: Middlewares configuration - Strapi Developer Docs)
module.exports = [
  // The array is pre-populated with internal, built-in middlewares, prefixed by `strapi::`
  'strapi::errors',
  'strapi::security',
  'strapi::cors',
   // ...
  'global::yourfile'
]
3 Likes