I added a mongodb custom database to my auth0 connection. It works well however doesn’t check against the username, just the email,
I attempted to edit the code as following, but get a callback is not a function error,
function login(email, username, password, callback) {
const bcrypt = require(‘bcrypt’);
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;
const client = new MongoClient(‘mongodb+srv://mern:’+configuration.MONGO_PASSWORD+‘@cluster0-2uci2.mongodb.net/?retryWrites=true&w=majority’);
client.connect(function (err) {
if (err) return callback(err);
const db = client.db('test');
const users = db.collection('users');
users.findOne({$or: [ {email: email }, {username: username }] }, function (err, user) {
if (err || !user) {
client.close();
return callback(err || new WrongUsernameOrPasswordError(email));
}
bcrypt.compare(password, user.password, function (err, isValid) {
client.close();
if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
return callback(null, {
user_id: user._id.toString(),
nickname: user.nickname,
email: user.email
});
});
});
});
}
Could someone please help? this is the original format the custom mongodb connection comes in:
function login(email, password, callback) {
const bcrypt = require(‘bcrypt’);
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;
const client = new MongoClient(‘mongodb://user:pass@localhost’);
client.connect(function (err) {
if (err) return callback(err);
const db = client.db('db-name');
const users = db.collection('users');
users.findOne({ email: email }, function (err, user) {
if (err || !user) {
client.close();
return callback(err || new WrongUsernameOrPasswordError(email));
}
bcrypt.compare(password, user.password, function (err, isValid) {
client.close();
if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
return callback(null, {
user_id: user._id.toString(),
nickname: user.nickname,
email: user.email
});
});
});
});
}