# Failure adding permissions to a user if signing via Google Oauth

**URL:** <https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563>\
**Category:** Get Help\
**Tags:** google-social-connection, connections\
**Created:** [June 19, 2023, 5:56pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563 "2023-06-19T17:56:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick25](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@nick25](https://community.auth0.com/u/nick25)\
**Post date:** [June 19, 2023, 5:56pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563/1 "2023-06-19T17:56:48Z")

</div>

Hi I’m encountering an issue where if a user signs in via google oauth they are not assigned a role but if they login via username-password they are assigned the role. I created a post user login action where they should be assigned the role after login. This only works when it’s a username-password authentication. Shouldn’t the code below apply to all authentications.

```auto
exports.onExecutePostUserRegistration= async (event, api) => {

  const ManagementClient = require("auth0").ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: 'update:roles'
  });

  const adminRole = { id :'SOME_ROLE'};
  var data = { "users" : [event.user.user_id]};

  try {
    await management.roles.assignUsers(adminRole, data);
  } catch (e) {
    console.log(e);
  }
 
};

```

I would appreciate some help.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [June 20, 2023, 6:52pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563/3 "2023-06-20T18:52:57Z")

</div>

Hi @nick25,

Welcome to the Auth0 Community!

Unfortunately, the Post-User Registration Action flow only runs after a user is added to a Database or Passwordless Connection. (Reference: [Post User Registration Flow](https://auth0.com/docs/customize/actions/flows-and-triggers/post-user-registration-flow))

Since Google is a Social Connection, we recommend using a Post-Login Action to add permissions to a Social Connection user.

For your convenience, I have updated your code snippet:

```auto
exports.onExecutePostUserRegistration= async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }
  const ManagementClient = require("auth0").ManagementClient;

  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret,
    scope: 'update:roles'
  });

  const adminRole = { id :'SOME_ROLE'};
  var data = { "users" : [event.user.user_id]};

  try {
    await management.roles.assignUsers(adminRole, data);
  } catch (e) {
    console.log(e);
  }
 
};

```

Please let me know if you have any questions.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![nick25](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@nick25](https://community.auth0.com/u/nick25)\
**Post date:** [June 20, 2023, 7:42pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563/4 "2023-06-20T19:42:29Z")

</div>

Thanks @rueben.tiow. Could you please elaborate on this modification? What’s the purpose of validating if the login count isn’t 1.

```auto
 if (event.stats.logins_count !== 1) {
     return;
 }

```

Also I tried what you mentioned but still fails to update the user with the role:

```auto
exports.onExecutePostLogin = async (event, api) => {
  const { user } = event;

  if (user.identities && user.identities.length > 0) {
    const googleIdentity = user.identities.find(identity => identity.provider === 'google-oauth2');

    if (googleIdentity) {
      const ManagementClient = require("auth0").ManagementClient;
      const management = new ManagementClient({
        domain: event.secrets.domain,
        clientId: event.secrets.clientId,
        clientSecret: event.secrets.clientSecret,
        scope: 'update:roles'
        });
      
      const adminRole = { id :'SOME_ROLE};
      var data = { "users" : [user.user_id]};
      
      try {
        await management.roles.assignUsers(adminRole, data);
        } catch (e) {
          console.log(e);
        }
      }
    }

};

```

Am I missing or doing something wrong here?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [June 23, 2023, 6:53pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563/5 "2023-06-23T18:53:34Z")

</div>

Hi @nick25,

Thank you for your response.

Sure, I would be happy to explain.

We are checking the condition if `logins_count` does not equal 1, because we want the script to execute only when `logins_count` does equal 1, indicating that the user has just signed up.

Take note that when a user completes the sign-up process, they are automatically logged in, which increments their `logins_count` by 1.

Circling back, could you please clarify if you are encountering any errors with your Action script or if the role is just not being updated?

And have you tried using the Actions built-in debugger to check if the code is being executed? You can add console.log statements to verify if the code is running as expected.

I look forward to your reply.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 7, 2023, 6:54pm UTC](https://community.auth0.com/t/failure-adding-permissions-to-a-user-if-signing-via-google-oauth/109563/6 "2023-07-07T18:54:30Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
