Tuesday 16 May 2017

Passport send callbacks for errors to be able to res.json

How can i catch errorfrom local-signup? i cant find a way to catch them.

The idea is to catch the errors, then send them as a message back to client with

res.json

Current output:

already taken????????????????????

node.js

router.post('/register', passport.authenticate('local-signup'),function(req, res, next) {
    console.log("registration");
    console.log("ERROR?");
    console.log(req);
    console.log(res);
   //   res.json({type: 'danger',message: info});


});

passport:

passport.use('local-signup', new LocalStrategy({
            usernameField : 'username',
            passwordField : 'password',
            passReqToCallback : true 
        },
        function(req, username, password, done) {
            process.nextTick(function() {
                console.log("doing local signup");
                Account.findOne({username :  username }, function(err, user) {
                    if (err)
                        return done(err);

                    if (user) {
                        console.log("already taken????????????????????");
                        return done(null, false, { message: 'That username is already taken.'});
                        return done(err);

                    } else {

                        var newUser            = new Account();
                        newUser.username    = username;
                        newUser.password = newUser.encryptPassword(password);
                        // save the user
                        newUser.save(function(err) {
                            if (err)
                                throw err;
                            return done(null, newUser);
                        });
                    }

                });

            });

        }));

Update:

I tried custom error callback code, but then i cant get req property to send request back to client.

I also tried to move the authenticate middleware to be called within the function req, res, next, but then it wont be called at all.



via maria

No comments:

Post a Comment