Google Auth Provider - get photo and name/surname

System Information
  • macOS Monterey 12.3:
  • strapi 4.0.7:
  • sqllite:
  • node 14.17.6:
  • yarn 1.22.11:

Hi everyone!

Is there a way to get more info (like name/surname and user photo) using standart stapi google provider, or i have to write custom provider?

If its all about “custom-way”, could anyone please provide me with some guide how to make it. Or maybe someone had similar experience?

Thanks in advance!

Hey take a look here : [Auth Providers] how to add scope for google - Questions and Answers / Strapi Backend - Strapi Community Forum

2 Likes

Thanks!

So i’ve created Providers.js and added bootstrap in index.js as in tread you linked.
But its not overriding providers from node_modules

Maybe i have to do something differently because im using v4 of strapi

Hi,

It’s possible so go to the source code to see where the file is located. Maybe it’s different

Also, only accounts création will use the New code, so maybe remove existing accounts and try again.

Hi!

I tried official guide + everything from the tread you linked (including your last answer about creating new user, cleaning user collection)

Added bootstrap.js and Providers.js as it mentioned:

extensions/users-permissions/services/Providers.js
extensions/users-permissions/config/functions/bootstrap.js

also added bootstrap function in src/index.js


  async bootstrap({ strapi }) {
    const pluginStore = strapi.store({
      environment: '',
      type: 'plugin',
      name: 'users-permissions',
    });
    // Ensure profile scope for Google Auth
    const grantConfig = await pluginStore.get({ key: 'grant' })
    if(grantConfig){
      if(grantConfig.google && grantConfig.google.scope){
        grantConfig.google.scope = ['openid', 'email', 'profile']
        await pluginStore.set({ key: 'grant', value: grantConfig });
      }
    }
  },

Still no result :cry:

May you try this :

case 'google': {
      const instance = axios.create({
        baseURL: 'https://www.googleapis.com/oauth2/v3',
        headers: {'Authorization': 'Bearer ' + access_token }
      });

      instance.get('userinfo').then(x =>  {
        callback(null, {
          providerId: x.data.sub,
          username: x.data.given_name,
          email: x.data.email
        });
      }, err => callback(err));
      break;
    }

In Provider.js

Any update on this one?

It think one problem is that the override files are not called and the other one is that even providing additional options to scopes like profile doesn’t really influence what data are returned. I have extended scoped to return openid and profile additionally and then console.log what will returned and I get only some token info. I have tried to change line 117 on @KevichVinc screenshot to userinfo but then I get 404, so I think it’s an invalid option.

By default response body equals to:

body: {
    issued_to: 'foobar.apps.googleusercontent.com',
    audience: 'foobar.apps.googleusercontent.com',
    user_id: '1234567890',
    scope: 'openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
    expires_in: 3598,
    email: 'foo@gmail.com',
    verified_email: true
  }

EDIT: thanks to this reply Changes for Google auth provider · Issue #2660 · strapi/strapi · GitHub, one need to change google URL in order to get profile info in the response as follows:

 google
        .get("https://www.googleapis.com/oauth2/v3/userinfo")
        .auth(access_token)
        .request(({body}) => ({
              username: body.email.split("@")[0],
              email: body.email,
              name: body.name,
              // put here your additional keys
        }))
1 Like

Version : 4.11.3

There is a functional code to add picture, first name etc in 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,
        };
      });
  },
};
3 Likes

Wow! You saved me a lot of time @Berrier_Joffrey
Great help
Thanks

1 Like

Great post. I would like to share these online services related to photographs:

https://depositphotos.com/music/music-sitemap-3.xml
https://depositphotos.com/music/music-sitemap-4.xml
https://depositphotos.com/music/music-sitemap-5.xml
https://depositphotos.com/music/music-sitemap-6.xml
https://depositphotos.com/music/music-sitemap-7.xml
https://depositphotos.com/music/music-sitemap-8.xml
https://depositphotos.com/music/music-sitemap-9.xml
https://depositphotos.com/music/music-sitemap-10.xml
https://depositphotos.com/music/music-sitemap-11.xml
https://depositphotos.com/music/music-sitemap-12.xml
https://depositphotos.com/music/music-sitemap-13.xml
https://depositphotos.com/music/music-sitemap-14.xml
https://depositphotos.com/music/music-sitemap-15.xml
https://depositphotos.com/music/music-sitemap-16.xml
https://depositphotos.com/music/music-sitemap-17.xml
https://depositphotos.com/music/music-sitemap-18.xml
https://depositphotos.com/music/music-sitemap-19.xml