Auth0 Home Blog Docs

How to create a user on the service side whenever a user register to my app through Auth0?

user-creation
hooks
registration

#1

I would like to create a user_table on my service side. This table will only contain user_id returned from Auth0, and a nick_name he would like to use in my app. See the following table example: a user registered through Auth0 using her Google account (Auth0 ID of google-oauth2|115015401343387192604), sets his nick_name in my app as “cute_user”.

Some users may not bother setting this up, if so, we will be using the “name” field returned from Auth0 (see the field at https://auth0.com/docs/link-accounts)


| user_id | nick_name in my app. |

| google-oauth2-115015401343387192604 | “cute_user” |

How could I create a new entry on my service side (i.e., AWS side) whenever a new user registers through Auth0?

@prashant @jerrie1 @jmangelo


#2

If your user_table will only store the user_id and nickname, have you considered just storing these directly in the Auth0 user profile? You could store a custom nickname that a user sets, in the user’s user_metadata, which would eliminate the need for your server side database.

https://auth0.com/docs/metadata

If you’d still prefer to have a database on your end, you have a couple of options:

  1. Post User Registration Hook - note, these hooks currently only run for database connections. They will not run for users that authenticate using a social provider, e.g. Google.
  2. You could create entries in your database inside a Rule. These will run during the authentication flow, so the entry can be created upon first login.

#3

@prashant

Thanks. Option 2 looks a great option to me (Option 1 is not because we use Facebook social login), I guess you meant “Notify other systems through an API when a login happens in real-time.” in https://auth0.com/docs/rules/current? Is there an example or a GitHub repo about this?


#4

This is what I do. I have a rule which runs on every login:

  1. Check the auth0’s user app_metadata for a property “service_user_id”. If the property is there, then I add it as a claim and proceed
  2. There is no property: so from the rule I call my service passing some data from the auth0’s user profile and auth0’s userId.
  3. On my service, I check if the user with that auth0 userId exists: if so, I just return my service’s userId. If not, I create the user on the service using some data received from the profile, and return the new userId
  4. Back on the rule, I set the “service_user_id” on auth0’s user app_metadata, add it as a claim an proceed

After login, a token is generated with my service’s userId as a claim.


#5

@prashant

Thanks. Option 2 looks a great option to me (Option 1 is not because we use Facebook social login), I guess you meant “Notify other systems through an API when a login happens in real-time.” in https://auth0.com/docs/rules/current? Is there an example or a GitHub repo about this?


#6

Have a look at some of the sample Rules:

Dashboard > Rules > Create Rule > Webhook section


#7

Thanks, glad that we can setup webhook in rule.

If the webhook call (to my service, to create a user item on my side) fails for whatever reason, let’s say that network between Auth0 and my service endpoint is partitioned, does Auth0 provide any mechanism to somehow do the webhook call to notify my service next time?

I know this sounds a paranoid ‘over design’, but I’d still like to get a high level idea about it.


#8

You could store an attribute in the app_metadata, e.g. external_user_created to false if the call to your endpoint fails. Your rule can then make this check, hence can make the call again the next time the user logs in.

if(!app_metadata.external_user_created) {
    //try again, then set user_creation_failed to false
    ...
    app_metadata.external_user_created = true;
    //update metadata
}

#9