I am using Auth0 for login in my Nodejs+MongoDB ap...
# random
e
I am using Auth0 for login in my Nodejs+MongoDB application. When user is authenticated, Auth0 provides user details such as name, email, nickname etc.(see attached screenshot) It also has one field called 
sub
 which is a unique identifier for that particular logged in user. Now I wanted to know should I store this 
sub
 field in mongo database schema for getting user specific data? for example if user creates some resources when logged In. Then while fetching data using /GET route I can use mongo query 
db.find({sub: xyz})
 and retrieve the data for that specific user. Is this correct implementation of storing
sub
field in database schema or do you think this can be improved? Please suggest thanks
m
Yup,
sub
is the user's id (as generated by auth0). You can also use a combination of
sub
+
email
.
e
So in my application there are posts which user creates. Now I have GET route for getting posts in that I am doing db.find({sub: xyz}) which returns all the posts for user with sub field having value xyz. Now I was thinking of another approach: when user login is success I will store user name, email, sub and _id (default created by mongodb) in mongodb user model. Now in post model I will have following fields: title, subtitle, and userId instead of sub field. Through this I can map user all the posts with specific user by userId. So in GET route I will do db.find({userId: userId}) instead of using sub field. Is this approach correct? Please suggest if this can be improved. Thanks
g
Yes, having your own ID for a user is better than relying on auth0 id. So in future you can swap out auth0 easily or add any other auth systems.
👍 1
e
You mean to say if in future auth0 APIs change and they remove sub field or rename it then my application backend will break if I run all queries on sub field instead of userId is that right?
👍 1
I have general question I am using Nodejs+Mongodb so in future I want to use my own authentication or want to add/update or delete fields then do I need to run migration up/down files in mongodb ? So schema migration is applicable in NoSQL and SQL databases is that right? migration is not specific to SQL only is that correct?