How do I store the logged in users in my own DB?

I’m using Django (DRF) as a rest backend and Nuxt on the frontend. I wish to store the logged in user in my DB because I have database tables that rely on the relation between a user and other tables. In the article https://auth0.com/blog/building-modern-applications-with-django-and-vuejs/

the author writes

For most cases, you don’t need to store users in your database since Auth0 handles all of that for you including advanced features such as profiles. You can use a custom function that checks if a general (can be fake) user that you create exists and map it to all Auth0 users.

What does he mean? I obviously need to know the logged in user so I can do the correct queries.

What is the solution here? How should I store the logged in user in my db? Or are there other ways?

Many of our Django apps use a database to store application-specific data. You can certainly do that. The point of the article is that you can store more or less whatever data you want in the user_metadata and app_metadata objects in the Auth0 user profile, which will be sufficient for many applications. But if you app needs a back-end DB, then absolutely do that.

I would strongly suggest creating a custom UUID attribute in your Auth0 user profiles and then use that UUID as your primary key in your application database. Or, if you don’t want to create a custom UUID, use the Auth0 user_id field. Don’t use email address or username (if you have usernames enabled).

1 Like

Thanks! Are there any guides on how this is done? I can’t be the only one who still needs a database in the backend?

I can’t speak to the specifics of Django + your chosen DB (we have a number of Auth0-enabled Django apps with Postgresql DBs) but in general: your app will receive an ID token for the user with identifying information in it like the sub, and you can add additional claims to the ID token, like a UUID if you implement that. Once your ID token has the data you need, use the sub or the UUID or whatever else from the token as your primary key.

If you want to create a UUID, I have some Rules at the link below that show how to create it and how to add it to your ID token. I haven’t looked at these in a while but they should still work.