# Post User object to SQL Server db after signup

**URL:** <https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596>\
**Category:** Get Help\
**Created:** [May 11, 2021, 2:49pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596 "2021-05-11T14:49:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rluka996](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rluka996/32/29245_2.png) [@rluka996](https://community.auth0.com/u/rluka996)\
**Post date:** [May 11, 2021, 2:49pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/1 "2021-05-11T14:49:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [May 11, 2021, 10:11pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/3 "2021-05-11T22:11:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rluka996](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rluka996/32/29245_2.png) [@rluka996](https://community.auth0.com/u/rluka996)\
**Post date:** [May 12, 2021, 12:40pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/4 "2021-05-12T12:40:50Z")

</div>

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](https://auth0.com/blog/complete-guide-to-angular-user-authentication). 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](https://this)) 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.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [May 21, 2021, 4:52pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/5 "2021-05-21T16:52:59Z")

</div>

> [@rluka996](#):
>
> So where exactly in my Angular front-end would I do that?

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?)

> [@rluka996](#):
>
> Would I have to connect auth0 to my local database somehow

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.

---

<div class="post-metadata">

**Author:** ![rluka996](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rluka996/32/29245_2.png) [@rluka996](https://community.auth0.com/u/rluka996)\
**Post date:** [May 21, 2021, 6:51pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/6 "2021-05-21T18:51:08Z")

</div>

> [@](#):
>
> 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, 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?

> [@](#):
>
> 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.

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?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [May 24, 2021, 4:12pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/7 "2021-05-24T16:12:20Z")

</div>

> [@rluka996](#):
>
> 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](https://auth0.com/docs/actions/triggers/post-user-registration) to send the user’s info to your backend API to be added to your database. Would that solve it?

---

<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 13, 2021, 4:31pm UTC](https://community.auth0.com/t/post-user-object-to-sql-server-db-after-signup/62596/8 "2021-07-13T16:31:44Z")

</div>

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