Post User object to SQL Server db after signup

So, I have an existing application which I’m trying to modify and use Auth0 as Authentication provider instead of storing password in my local database. User object has multiple relationships so I still need my local database, right?
My main question is, how do I post a User object to my local database after User successfully signs up? (Without password, ofc)
I am using Angular as a frontend, .net core api as a backend and SQL Server as my local database.

My guess is towards Rules, Hooks and Database connections but I’m just overwhelmed.

Hi @rluka996,

Welcome to Auth0!

The answer will depend on a few things, as there are a few ways to create a relationship between the Auth0 data store and your backend DB.

The user_id issued by Auth0 can act as the link between the user’s token that is returned from Auth0 and your database. When you create a new user in your database, store the sub claim from the token (this is the user_id) to use as a UUID.

A few things to consider:

  • You can create new users when they don’t exist in your DB. When your app receives a token and verifies it, you can use the user_id (sub in the token) to look up the user in your backend DB. If the user doesn’t exist, you will know they are a new user and you should create a row/object for them.

OR

  • You can use an action (the new version of rules/hooks released to GA today) to create a user in your backend DB via a POST request.
  • A post-registration action runs after a new Auth0 database or passwordless user is created. You can code an action that will call your backend database. This will not run when a social user logs in for the first time (google/linkedin/twitter/etc.)

Does that make sense?

Yes it does! Just a couple of questions if you don’t mind.

First option:
So where exactly in my Angular front-end would I do that? It’s kind of unclear to me where in code does that happen, I followed this guide. And every time user logs in/signs up I would extract user_id from token and check if that object exist in my db?

Second option:
Yeah, I just saw an action as a new option in Dashboard. Would I have to connect auth0 to my local database somehow(something like https://auth0.com/docs/connections/database) or just writing an action is enough? Also, is there a example of that kind of action?

Sorry for bothering you, I’m not very familiar with auth0, hope it’s not a problem.

Are you making requests to your backend API to request user information? (for example, if you were running a blog, when are you request the user’s blog post data to display in your angular app?)

Yes, you could make a post request to your backend API to create a user in your database. This means you have to have some mechanism to receive that request. With a SPA, you usually have a backend API that is handling this type of thing.

Yes, I am. It’s a pretty complex database for application like Skyscanner with Users and their bookings, reviews, friends etc. So currently, I have auth0’s callback url set to a certain url and in that component(that url) I’m posting User object to API like this:

this.auth.user$.subscribe(token => {
this.User= token;
});

and then I’m doing this:

let newUser = new NewUser({
** ExternalId: this.myToken.sub,**
** Email: this.myToken.email,**
** Name: this.myToken[‘metadata’].name,**
** Surname: this.myToken[‘metadata’].surname,**
** City: this.myToken[‘metadata’].address,**
** PhoneNumber: this.myToken[‘metadata’].phoneNumber**
** });**
this.userService.createUser(newUser).subscribe();

On the API service I’m checking if user already exist(if it does it’s a login, and I skip adding it to database, if it doesn’t its a signup and I add him).
But I’m pretty sure this isn’t the greatest solution. What would you recommend?

Could this be done by simply triggering an action in which I would send User object with fields required and then do all the work needed on actual API?

You could use a post registration action to send the user’s info to your backend API to be added to your database. Would that solve it?

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