I have an APP where I am using Passport to authenticate and register users. I have set up mongoose validation for my schema.
For example:
// # === Business Bio Validation === #
var businessBioValidator = [
validate({
validator: 'isLength',
arguments: [2, 140],
message: 'Business Bio should be between 3 and 140 characters'
})
];
And within my schema I have
businessBio: {
type: String,
required: true,
validate: businessBioValidator
}, // only 140 characters
Now if this validation is not met I want to send the message in my validator as a Json response. If I put the code below within my actual "route" it works!
return provider.save(function (err)
{
if (!err) {
console.log("updated");
} else
{
return res.send(err);
}
return res.send(provider);
});
});
But I can't send err response from my Passport authentication. See code below:
function(req, email, password, done) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
console.log('using providers-signup passport');
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
Provider.findOne({ 'email' : email }, function(err, provider) {
// if there are any errors, return the error
if (err)
return done(err);
// check to see if theres already a user with that email
if (provider) {
return done(null, false, { message: 'User email already taken.'
});
} else {
// if there is no user with that email
// create the user
var newProvider = new Provider();
// CREATE USER HERE!
// save the user
newProvider.save(function(err) {
if (err)
{throw err;}
return done(null, newProvider);
}); }
});
});}));
Right now when I create a user through passport all my error are console logged on the node sever. How can I send the error messages from my passport authentication as JSON message response to the user.
Thanks in advance.