How to override Users-Permissions on V4

Hello,

I try to verify recaptcha before register but i can’t override users-permissions,

Thanks for any suggestions !

1 Like

Maybe you can extend users-permissions like this: https://smoothdvd.medium.com/add-a-customize-users-permissions-provider-for-strapi-v4-6aa78c642977

1 Like

Thank you !
I find an easy way to verify captcha:

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

const axios = require("axios");

module.exports = (plugin) => {
  const register = plugin.controllers.auth.register;

  plugin.controllers.auth.register = async (ctx) => {
    ctx.request.body.confirmed = false;
    const token = ctx.request.body.token;
    const gres = await axios.post(
      `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.GOOGLE_SITEKEY}&response=${token}`
    );
    console.log(gres.data);
    if (!gres.data.success) {
      return ctx.badRequest(
        null,
        formatError({
          id: "Auth.form.error.token.provide",
          message: "Please provide a valid token.",
        })
      );
    }
    await register(ctx);
  };
  return plugin;
};
4 Likes

Great work!

1 Like

Thanks so much, I was breaking my head over this!
One detail, it’s obviously the secret you have to pass in to the url , not the sitekey as you have it

Sorry, but I couldn’t see something effect in my app.
I used Strapi with typescript and create strapi-server.js file like you :slightly_frowning_face:

@Nguyen_Nguyen If you use typescript, try using strapi-server.ts
Also try to remove the cache and dist folder
Also try to build the project again

At the last resource, try pulling the new source code and try modify the extension, you will see it work in the new source code and try comparing with your current one

Hope this help!

1 Like

Version : 4.11.3

There is a functional code to rewrite the users-permissions provider, if you want to get picture, first name etc from Google and using the Google sign-in with the Strapi provider

Put this code in the src/index.js

"use strict";

module.exports = {
  register(/*{ strapi }*/) {},
  async bootstrap({ strapi }) {
    await strapi
      .service("plugin::users-permissions.providers-registry")
      .register(`google`, ({ purest }) => async ({ query }) => {
        const google = purest({ provider: "google" });

        const res = await google
          .get("https://www.googleapis.com/oauth2/v3/userinfo")
          .auth(query.access_token)
          .request();

        const { body } = res;

        return {
          email: body.email,
          firstname: body.given_name,
          lastname: body.family_name,
          picture: body.picture,
          provider: "google",
          username: body.name,
        };
      });
  },
};

Hello there. I would like to send an email when user information is changed.
I have tried creating strapi/src/extensions/users-permissions/strapi-server.ts:

module.exports = (plugin) => {
  plugin.controllers.user.update = async (ctx) => {
    console.log(ctx.request.body);
  };
  return plugin;
};

But it does not work. Changing user-data on admin-panel there is no console.log.

1 Like

The solution is to modify the file /strapi/src/extensions/users-permissions/strapi-server.ts. The original source could be found inside users-permissions inside node_module. After update, run npm install to update.

saved my day! I was actually using .js file in a .ts project, didn’t see any logs printed for all the customisations I added, pulling my hairs until I saw this one.Thank you!

in production mode user information was reduced to the essential user information includes the user’s email, picture, and email verification status, how should i do to retrieve all the above data?

In source code, you can use entityService to fetch from database. Via API you may try “populate”.

mean, how to oblige the “https://www.googleapis.com/oauth2/v3/userinfo” in production mode to return more than the essentials user info.