Отправка пользовательских данных json после регистрации на локальный паспорт - PullRequest
0 голосов
/ 04 января 2019

Я использую локальный паспорт для регистрации пользователей. Я не совсем понимаю, как отправлять пользовательские данные json вместо перенаправлений в паспорте. в основном я хочу, чтобы passport отправлял пользовательские сообщения после оценки успехов / неудач и позволял следующему обработчику маршрута брать данные JSON и предоставлять их пользователям. Вот мой код для паспорта:

passport.use('local-signup', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, email, password, done) {

        // 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
        User.findOne({ 'local.email' :  email }, function(err, user) {
            // 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 (user) {
                return done(null, false,{message: "Email is already taken "}) //req.flash('signupMessage', 'That email is already taken.'));
            } else {

                // if there is no user with that email
                // create the user
                var newUser            = new User();

                // set the user's local credentials
                newUser.local.email    = email;
                newUser.local.password = newUser.generateHash(password); // use the generateHash function in our user model

                // save the user
                newUser.save(function(err) {
                    if (err)
                        throw err;
                    return done(null, newUser);
                });
            }

        });

    }));

Вот мой обработчик маршрута:

router.post('/api/signup', passport.authenticate('local-signup'), (req, res) => {

        const response = {};
        response._id = req.user._id;
        response.email = req.user.local.email;
        res.status(200).send(response);


});

прямо сейчас ответ отправляется, только если он успешен, но если он терпит неудачу или получает какую-либо ошибку, паспорт обрезает запрос и отправляет 401 неавторизованный и не позволяет следующему промежуточному программному обеспечению контролировать входящий запрос. Как мне переопределить это поведение?

...