Auth0 Home Blog Docs

Getting information from custom database

api
custom-database
#1

am I able to set up a rule to display/get more information for my user from my custom database using my user_id from users table as a foreign key?

#3

Rules can perform outgoing network requests so from a technical point of view it is possible to obtain data from an external system for a specific user as long as there’s a field available to rules and that clearly identifies the user in the external system.

You can do the above by having an HTTP API brokering this conversation or you can even see if using modules listed at (https://auth0-extensions.github.io/canirequire/) you are able to directly connect to the database engine.

Having obtained the information you can then store it as part of the user profile information (see https://auth0.com/docs/rules/guides/metadata).

#4

I’m directly connected to my database. That’s how Auth0 Authenticated with my app. But it seems like it’s not authenticating with my backend. It finds the user and it displays the info that auth0 has when created the account but it seems like it’s not reading my backend.

#5

You should provide additional context about the scenario, for example:

  • sample code for the rule.
  • the user attributes for which you’re not receiving up to date information.
  • how the client application is making the authentication request.
  • where do you see the stale data? (in an ID token, an endpoint response, etc)
#6
  • Well I don’t have any rules at the moment. This was the first time I was suggested that option.
  • The user attributes which I want to be receiving are 5 tables with each having 4 columns of data.
  • It’s using my database as an identity provider. auth0 checks users table inside my own MySQL database and checks if the user is already registered by email.
  • The data that the user uses to register is shown in the ID token.
#7

Your original question is phrased around rules so I assumed you wanted to use rules. In an interactive authentication transaction associated with a custom database (without import mode) there are two extensibility approaches that can be leveraged to get more data into the user profile (rules or the actual login script).

If we take rules out of the picture we still need to have a login script, so what have you already tried in the login script in order to return updated information? The script should return a user profile on success and that profile can contain metadata as mentioned at (https://auth0.com/docs/connections/database/custom-db/templates#script-template-checklist). This means that at each interactive login you can get up-to-date information from your custom database into the Auth0 user profile.

#8

So you’re able to modify the Login Script template given to me by Auth0 to return data in my database like example: profile_education table that I created?

  • by that I also mean that I have not modified the Login script and still have it the same as given to me by Auth0.
#9

Yes, that is correct, you can modify the login scripts and customize it to meet your requirements within the supported capabilities of the service. The default templates are just that, a template to help you get started, but can then be modified to meet custom demands.

#10

Thanks, @jmangelo This is the first time I hear of this! I’ll definitely be trying this.

I’m also using my user_ id as a foreign key in my “profile_education” table, so when modifying the script, I’ll be calling that foreign key correct? (this is not a MYSQL question, I just wanna know if this is the correct way of doing this)

#11

I don’t want to lead you in error as I don’t have the visibility into all your system, but yes, it’s technically possible to use user_id as foreign key as that is an identifier unlikely to change. If you use account linking than you will get into a situation where two users are merged into one so you’ll have to decide which user identifier wins and that may impact foreign keys, but that is a very particular situation.

#12

I won’t be using account linking but thanks for the heads up.

So when I modify the scripts and have the data that the user is related to, 'll have to display the information through auth0 front-end just like how I am displaying the email address?

How do I make Auth0 communicate wit my backend REST API?
#13

The information you decide to return in the custom script can be stored in the Auth0 user profile metadata (https://auth0.com/docs/users/concepts/overview-user-metadata) so yes, it will be available in the same place as emails and can be provided to client applications.

Just have in mind that not all data is suitable to be stored like that; see (https://auth0.com/docs/best-practices/user-data-storage-best-practices) for some guidelines.

#14

Would I have to modify the create script? When I modify the login script its pulling and displaying the data. but now when I run the create script it throws me this error:

“We’re sorry, something went wrong when attempting to sign up”

but it still creates the user in my database but it doesn’t let them sign in.

#15

For the purpose of getting data from your custom store into the Auth0 user profile there may be no need to touch the create script as that script is just for signup and the retrieval of data would happen on the login script. However, the create script still needs to handle the signup properly and meet the requirements for the signup to be considered correctly.

The default template for the create script should inform you how the script should return for a success operation; if you don’t have the default template at hand just create a second database connection and you can check the default template in that one and then delete it.