Auth0 Tutorials: Using MongoDB Atlas as a Custom Database

In this article, you will learn how to use MongoDB Atlas as a custom database in your Auth0 account.

Brought to you by @bruno.krebs :man_technologist:t5:

Read on :dart: >> Connecting Auth0 to MongoDB with Custom Databases

Quite easy to integrate these great services together, right? Let me know your thoughts.

Hi,

Thank you for your tutorial. I was able to implement all the custom functions except one. In ā€œcreateā€, Iā€™m getting an ā€œUser not foundā€ error. This looks counter-intuitive, as users are not expected to be found before sign up. Further, I cannot log anything, when I can with any other custom functions. Could you please advise? Let me know if you need any other information.

image

image

Adding to my previous comment, it seems like when I call create(), Iā€™m unknowingly calling getByEmail(). The ā€œUser not foundā€ error was coming from getByEmail() based on my experiment using console.logs.

Sorry for the spam, but just wanted to give you an update. Iā€™ve wiped out getByEmail(), and now create() works for the first time. I can resort to not using getByEmail() and updating the logic for changePassword() to check an user exists; however, it would still be great if you could update the guide for future references.

hi, @quizapp6781. Sorry for the delay. So, you solved your problem by not using the ā€œGet Userā€ script? I will have to double check this to see if there is a problem on the article instructions.

Hi @bruno.krebs. No problem!

Yes, I could sign up users by not using the ā€œGet Userā€ script. I realized that the ā€œGet Userā€ script is called when I call ā€œSign upā€. Auth0 could not, of course, ā€œGet Userā€ when the user still hasnā€™t signed up yet (and thus not in the database).

1 Like

I see, thanks for letting us know.

Hi, @quizapp6781. I think I know what the problem is. Let me see if I get this right:

You probably started reading the article at the very top and implementing the version of the integration that migrates users to Auth0, right? Then, following the article, you switched to the alternative that does not migrate users. However, instead of replacing the Get User script with the one showed on this section, you probably kept the old one there.

If you did that (and the message you were getting makes me believe you did), then that was the problem.

Although subtle, there is a difference on both scripts. The first one contains the following conditional:

if (!user) return callback("User not found.");

The second one contains the following:

if (!user) return callback(null);

This difference allows the Create script (sign up) to keep moving if there is no user on the database. Oh, and to make things a bit clearer. This works like that because Auth0 wants to check if the user that is trying to sign up already exists on your database (to avoid duplicates). That is, the Create script calls the Get User script to make this validation.

Let me know if that helps and solves your problem.

Great tutorial. It works just fine.

But shouldnā€™t it save the passwords with a bcrypt hash?

Hey there, @BrunoSette. Welcome to our community and thanks for reaching out to us.

Sorry for the delay, I lost track of this thread. You are right about using bcrypt or similar. As the article says:

Note: The script above is considering that you are persisting passwords as plain text in your MongoDB Atlas cluster. This approach is not recommended in any situation and it is used here just to enhance readability and to smooth the learning process.

So, the idea there is just to make things more readable. But you should never store passwords as plain text.

You will get ā€˜User not foundā€™ when it tries to login in if you donā€™t have usernames enabled in your auth0 database connection. If you donā€™t want to use usernames just change any of the scripts that are looking for ā€˜usernameā€™ to ā€˜emailā€™

1 Like

Thanks for the knowledge Corey!

1 Like

Great tutorial but running into this error message with MongoDB Atlas: