Jesus Soto Jesus Soto - 2 years ago 114
AngularJS Question

How to show failure message on signup using MEAN stack

I am developing an SPA which use passport authentication to do the login and signup proccess. The thing is that everything works fine, I can even show a success notification, but I can't find out a way to show an error notification in case the user makes a bad operation.

Here is my code for signup in passport-init:

passport.use('signup', new LocalStrategy({
passReqToCallback : true
function(req, username, password, done){
var email =;
{username: username},
{email: email}
}, function(err, user){
return done(err, false);

if(user) {
console.log('username or email already taken '+req.body.username);
return done(null, false, {message: 'Username or email already taken.'});

user = new User();

if(req.body.password===req.body.username || req.body.password===req.body.fullname
console.log('The password can not be equals to your fullname'+
', username or email. Try another password.');
return done(null, false, {message: 'The password can not be equals to your fullname'+
', username or email. Try another password.'});

Then if non of this cases happens the method returns no errors with the user data.

Here is the route code to pass results to angular:

router.get('/failure', function(req, res){
res.send({state: 'failure', user: null, message: "Invalid username or password"});
});'/signup', passport.authenticate('signup',{
successRedirect: '/auth/success',
failureRedirect: '/auth/failure',
failureFlash: true

And finally I got this angular in order to call and get a response:

$scope.register = function(){
$'/auth/signup', $scope.user).then(

$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$rootScope.signUp = true;

function(data, status){

This is it. Ofcourse the angular code is inside a controller. I just began learning to build MEAN stack applications so, any help will be appreciated.

Answer Source

If there's a custom error/ custom logic you should send an error back to the client:

return done({error:'userAlreadyTaken'},false);

the same for the other conditions you have in your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download