Population of MongoDb Document with Auth0 User

Hey guys, I’m just trying Auth0 for the first time and must say that I’m impressed so far.

Still, one thing bothers me quite much, especially when it comes to performance. I would really appreciate some input for inspiration.

Imagine the following.

Users are stored via Auth0 (Regular & Social Logins)
Content for an SPA is stored in an own hosted MongoDb

Imagine a simple Social Network.

1. Scenario 1. do not know how to handle the best way

Users create e.g. Feed posts that in MongoDb easily would contain a ObjectId property defining the post owner. You can then easily populate feed posts with users from the users collection to show their profile image or names. Straight forward.

How would you create the mapping between the not-ObjectId-type users from Auth0 with MongoDb. As a simple string? Then you might miss some MongoDb functionality and it seems a bit unhandy.

2. Scenario 2. do not know how to handle, especially considering performance

Let’s say you want to render 30 feed items. For each feed item you need to either populate the user on server side, which is not possible as the user data is not stored in the MongoDb (Also Auth0 custom databases do not help here because only non-social users go there) or you request each single user from Auth0 afterwards on the client side on rendering the components. Still then you need to provide your API Token to do a request or route it via your server, which creates even more overhead. Or you populate the users by doing requests to Auth0, but that does not seem to be performant either.

So the whole question is about: How do you do performant mappings / populations / joints between Auth0 users and your actual content.

Cheers, Felix :slight_smile:

Hi @felix.melchhardt,

Welcome to the Community!

Thanks :blush:

Auth0 provides a unique ID for each user. which is the sub claim in the id token or access token. When the user logs in, Auth0 will return the tokens, and you can lookup posts by the sub/user_id.

You can create a corresponding user object for each user that signs in. When you get a token, use that same user_ID/sub to look up the user or create a new one, with the user_ID as a key. Auth0 should be handling authentication-related data, but you may still want an object for user data in mongo db. Anything not authentication related; blogs, posts, comments, preferences, will be stored outside of Auth0.

Hey @dan.woda,

thank you for the fast reply. I just made a small Draw.IO diagram but I think I got you right. :slight_smile: I think you would have anything documented but especially in POC phases and to check which services to use, there is not always the time to dig into the whole documentation. So thank you for the response! :slight_smile:

It’s possible to do what you posted with a post reg hook, but that hook only runs on auth0 DB connection signups (no social users).

I was suggesting something more like this.

Untitled Diagram

1 Like

Thank you very much, that helps a lot! :slight_smile:

1 Like

A post was split to a new topic: React, Express, and MongoDB Setup