I am using mongodb and mongoose to handle my database needs. I currently have this:
mongoose.connect(secrets.db, {server:{poolSize: 1}});
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});
As my mongo connection. I want to change that so that each user that logs in (using passport) gets their own database. So when I do show dbs
I would like to see user_ObjectID_database_name
how can I modify my code to get this result? I want it because I believe it is more secure from my research. I can not however find any good guides on how to do this. I am sure they exist but I have spent 4 ish hours looking with no luck on even a semi-okay one that I could guess at.
Currents I just have a users
collection.
This is my current code that runs when a user hit /signup
as a POST
request.
exports.postSignup = function(req, res, next){
req.assert('email', 'Please sign up with a valid email.').isEmail();
req.assert('password', 'Password must be at least 6 characters long').len(6);
var errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
req.flash('form', {
email: req.body.email
});
return res.redirect('/signup');
}
// calls next middleware to authenticate with passport
passport.authenticate('signup', {
successRedirect: '/dashboard', // Select redirect for post signup
failureRedirect: '/signup',
failureFlash : true
})(req, res, next);
next();
};
I call this as a middleware in my route to keep them cleaner.
EDIT **
Would sharding be a better solution for performance? I know it requires a lot of set up and planning but it might be worth it? Only question is security...