User creating through google auth

I am using google auth2 for social connection and if any user not in auth0 database try to login it is creating new user without user role… So, finally I need to create a user who received invitation that’s it.

how can I do this? and also I am using just free version of auth0

Hi @prasanthi

Welcome to the Auth0 Community!

From what I understand, you would like to assign a newly signed user via google social connection to the specific role.

One way to achieve this is to use the Action with Post-Login trigger.

exports.onExecutePostLogin = async (event, api) => {
  if (event.stats.logins_count !== 1) {
    return;
  }

  const namespace = "https://my-app.example.com";

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

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

  const defaultRole = { id :'YOUR_DEFAULT_ROLE_ID_HERE'};

  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    await management.roles.assignUsers(defaultRole, data);
  }

With this Action, each new user will be assigned to the defaultRole which you I assume already created in the Auth0 Dashboard.

In case you would like to select only google social connection user you need to extend if to the:

if (event.authorization && event.connection.name==="google-oauth2") event.

I hope it will help you
Thanks and have a great day!

Dawid

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