Signup with google-oauth2 while passing user_metadata variables

I am using auth0.js web library

I have implemented passing promo codes through user_metadata for sign up with email without any issue.

However I cannot figure out how to implement it for connection: 'google-oauth2'

Here is my webAuth athorize block:

webAuth.authorize({
  connection: 'google-oauth2',
  user_metadata: {
    promo_code: promoCode
  },
  redirectUri: redirectUri,
},

However I cannot seem to get it to work using google-oauth2
If I try and use webAuth.signup instead of webAuth.authorize it says I need to pass an email and password.

When I use this code, the user is able to sign up no problem however the user_metadata is not storing the promo code the user signed up with therefore being charged full price.

I actually just went through this, Iā€™m using the auth0.js library for a custom signup form where users can signup with email/pass or through social.

Basically once a user signs up you need to grab their token, and then hit the management api where you can do whatever you want to the users profile.

Most of this I got from the piecing together info from this page:
https://auth0.com/docs/libraries/auth0js#getting-the-user-profile

Here is how I got metadata added to a social users login:

function google(){
    webAuth.popup.authorize({
        redirectUri: 'https://example.com',
        connection: 'google-oauth2',
        audience: "https://example.com/api/v2/",
        scope: 'openid profile email read:current_user update:current_user_metadata'
    }, function(err, authResult) {
        let token = authResult.accessToken;
        webAuth.parseHash({ hash: window.location.hash }, function(err, authResult) {
            if (err) {
                return console.log(err);
            }
            webAuth.client.userInfo(token, function(err, user) {
                // Now you have the user's information
                console.log(user);
                console.log(err);
                var auth0Manage = new auth0.Management({
                        domain: 'example.auth0.com',
                        token: token
                    });
               let metaData = {promo: '1234', date: date};
                auth0Manage.patchUserMetadata(user.sub, metaData, function(err, user) {
                    console.log('finished');
                    console.log(user);
                    console.log(err);
                });
            });
        });
    });
}

Hi @Chris1337,

Iā€™m doing the above in a hosted login page. My code is as follows:

function loginWithGoogle() {
  webAuth.authorize(
  {
    connection: 'google-oauth2',
  },
  function (err, authResult) {
    webAuth.parseHash(
       { hash: window.location.hash },
         function (err, authResult) {
           let idToken = authResult.idToken;
           if (err) {
             return console.log(err);
           }
           webAuth.client.userInfo(token, function (err, user) {
           var auth0Manage = new auth0.Management({
             domain: 'DOMAIN',
             token: idToken,
           });
           let metaData = { promo: '1234', date: date };
           auth0Manage.patchUserMetadata(
             user.sub,
             metaData,
             function (err, user) {
               console.log(err);
           });
        });
    });
  });
}

This doesnā€™t seem to do anything though. I canā€™t get any Google userā€™s to actually have User Metadata. Iā€™ve tried playing with the audience that gets supplied to webAuth so that itā€™s for the Management API but I canā€™t seem to make this work. Any ideas? Any help would be greatly appreciated.

Previous message deleted due to SPAM reasons.

Hi @Chris1337, thanks for sharing this charming solution, the authResult in my callback function is undefined, do you have any ideas?
Also seems like this is for popup authorize, does it work for none popup version?
Just like webAuth.authorize()?
Iā€™d be very appreciate

1 Like

I tested it with webAuth.authorize() and it doesnā€™t work for me. Am I doing something wrong or is your example @Chris1337 only for the popup version?

Hi @ArkasDev - I was having the same issue as you, while I have not tried the above solution, I did get something to work for me. Basically if you pass what you need as an extra query param in your /authorize endpoint, you can access that value from within a post login action and set the user metadata like this:
exports.onExecutePostLogin = async (event, api) => {
if (event.connection.strategy === event.connection.name && event.stats.logins_count === 1) {
api.user.setUserMetadata(ā€œmobile_phoneā€, event.request.query[ā€˜mobilePhoneā€™]);
}
};
For me it was mobile phone. Hope this helps