Auth0 Home Blog Docs

How To Set Up Custom Database Connections [Video Walkthrough]

If you have your own user database, you can use it as an identity provider in Auth0 to authenticate users. In this tutorial, we’ll show you how to create and configure a custom database connection using the Auth0 dashboard and mLab.

Let us know if you have questions or feedback on this topic!

2 Likes

Hi, I tried to follow your example, however, I couldn’t find the given application code (mongodb://…). I’ve only found the one where it says ‘…mongo.db.net/test?retryWrites=true’. I did put that into the script but It may have caused the failed attempts.

Also when editting documents, that feature is not a simple in the new Mongo site. I cannot write the code you have shown in the video as it can only be edited line by line. I had to figure it it’s an object or script which I am not knowledgable about so incorrect sorting may have caused the failed attempt.

Short story short, if you could please make another video of this but acting on the new mongodb website. It would be much appreciated, Thanks! Sorry for the inconvenience, not too exposed to coding.

Hi @castgiane! Thanks for letting us know about the new Mongo site updates. I’ve passed this feedback to our Developer Success team, and we’ll let you know once we review the issues!

Hi @castgiane , Thanks for the info. You can use the mLab to create a test database.

https://mlab.com/login/

For simplicity, I am attaching the script used in the video.

Login Script:

function login(email, password, callback) {
    mongo(`mongodb://${ configuration.user}:${ configuration.pass}@[copy database info from mLab ] `, function (db) {
        var users = db.collection('users');
        users.findOne({
            email: email
        }, function (err, user) {

            if (err) return callback(err);

            if (!user) return callback( new WrongUsernameOrPasswordError(email));

            if (password !== user.password) {
                return callback();
            }

            callback(null, {
                user_id: user._id.toString(),
                nickname: user.nickname,
                email: user.email
            });

        });
    });
}

Get User Script:

function getByEmail(name, callback) {
    mongo(`mongodb://${ configuration.user}:${ configuration.pass}@[copy database info from mLab]`, function (db) {
        var users = db.collection('users');
        users.findOne({
            email: name
        }, function (err, user) {

            if (err) return callback(err);

              if (!user) return callback( new WrongUsernameOrPasswordError(email));

            callback(null, {
                user_id: user._id.toString(),
                nickname: user.nickname,
                email: user.email,
                
            });

        });
    });
}

Test User document:

{
      "email": "test@auth0.com",
      "password": "yoursecurepassword",
      "nickname": "testUser",
}

You mentioned about new mongodb website. Are you referring to mongoDB Atlas?

2 Likes

Hi @castgiane, if you’re using mongoDB Atlas, this thread on Twitter may help you: https://twitter.com/sagarbetkar/status/1111582850069860358