How to overwrite auth0 provider method found in `getInitialProviders()` - @strapi/plugin-users-permissions

Solved: :white_check_mark:

After revisiting the docs and the code I sorted out that I can just overwrite the auth0 provider in the project register() function.

I sorted out the service I needed to request via the CLI:

npm run strapi -- services:list 

Then I register a replacement for the auth0 provider with my own version.

import type { Strapi } from '@strapi/strapi';

export default {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register( { strapi }: { strapi: Strapi }) {
    strapi
      .service('plugin::users-permissions.providers-registry')
      .register(`auth0`, ({ purest }) => ({ access_token, providers }) => {
        console.log(`My Auth0`)
        const auth0 = purest({ provider: 'auth0' });

        return auth0
          .get('userinfo')
          .subdomain(providers.auth0.subdomain)
          .auth(access_token)
          .request()
          .then(({ body }) => {
            const username = body.username || body.nickname || body.name || body.email.split('@')[0];
            const email = body.email || `${username.replace(/\s+/g, '.')}@strapi.io`;

            return {
              _raw: {...body},
              username,
              email,
            };
          });
      })
  },
};

… now I just have to try to improve the types and write a Unit Test… :wink:

3 Likes