Additional profile info from Google Auth Provider in Strapi V4

System Information
  • Strapi Version: 4.10.2
  • Operating System: macOS
  • Database: Postgres
  • Node Version: 18.12.1
  • NPM Version: 9.6.5
  • Yarn Version:

Finally successful in retrieving additional profile information from Google Auth Provider and thought I would share. First, thank you to the others that showed code for overriding the users-permissions plugin files. There is a full code example for doing that from yuichiromukaiyama found here: code

After installing this code, I was able to override the google provider in provider-registry.js like this:

  async google({ accessToken }) {
    const google = purest({ provider: 'google' });

    // return google
    //   .query('oauth')
    //   .get('tokeninfo')
    //   .qs({ accessToken })
    //   .request()
    //   .then(({ body }) => ({
    //     username: body.email.split('@')[0],
    //     email: body.email,
    //   }));

   return google
    .get("https://www.googleapis.com/oauth2/v3/userinfo")
    .auth(accessToken)
    .request()
    .then(({ body }) => ({
          username: body.email.split("@")[0],
          email: body.email,
          firstName: body.given_name,
          lastName: body.family_name
          //avatar: body.picture
        })); 
},

Note that in my user model I have ‘firstName’ and ‘lastName’ fields defined. I also have an ‘avatar’ media field but I have not been able to figure out how to populate it with the Google picture url. I know that media fields are defined as related data elsewhere so it is a little more complicated.

Hope this helps others.

2 Likes

Thanks for sharing, good work.

@cajazzer May i know the file path where you have added this code does it is src/index.js? and does it will be aplicable with todays version of strapi please confirm it