如果护照返回了用户{状态:200}:

护照

...
return done(null, rows[0], { status: 200 });
...


我希望调用控制器“ controllerLogin.login”:

溃败/index.js

const express = require('express');
const passport = require('passport');
const passportConf = require('../passport');
const controllerLogin = require('../controllers/login');
...

router.route('/v1/login')
    .post( function(req, res, next) {

        passport.authenticate('local-login', function (err, user, context = {}) {
            if (err) {
                console.log(err);
            }
            if (context.status === 429) {
                return res.status(429).send({ status: 429, success: false })
            }
            if (context.status === 401){
                return res.status(401).send({ status: 401, success: false })
            }
            next();
            //return;

        })(req, res, next);

}, controllerLogin.login );


但是我无法访问控制器'controllerLogin.login'。我缺少什么以及如何执行“ controllerLogin.login”?

下面的工作,但我需要较高的版本。

const passLogin = passport.authenticate('local-login', { session: false, failWithError: true });

router.route('/v1/login')
    .post( passLogin, function(err, req, res, next) {
        return res.status(401).send({ status: 401, success: false })
}, controllerLogin.login );


编辑:什么工作...

router.route('/v1/login')
    .post( function(req, res, next) {

        passport.authenticate('local-login', { session: false, failWithError: false }, function (err, user, context = {}) {
            if (err) {
                console.log(err);
            }
            if (context.statusCode === 429) {
                return res.status(429).send({ status: 429, success: false, message: { name: 'Rate Limit Error' } })
            }
            if (context.statusCode === 401){
                return res.status(401).send({ status: 401, success: false, message: { name: 'Authentication Error' } })
            }

            // this works getting user information
            console.log('user:');
            console.log(user);

            next();

        })(req, res, next);

}, /*controllerLogin.login*/ (req, res) => { res.status(200).json({just: 'testing'})} );


控制器/login.js

module.exports = {
    login: async (req, res, next) => {

        // Can't access user information via 'req.user' anymore
        console.log('req.user:');
        console.log(req.user);

        /* .. How to access user information here?  .. */

        res.status(200).json({just: 'testing'})

  }
}

最佳答案

听起来好像controllerLogin.login要req.user,但是尚未设置。因此,请尝试在传递给护照的authenticate函数的回调中手动执行此操作。

router.route('/v1/login')
    .post( function(req, res, next) {

        passport.authenticate('local-login', { session: false, failWithError: false }, function (err, user, context = {}) {
            if (err) {
                console.log(err);
                return next(err); // might want to add this line to handle errors?
            }
            if (context.statusCode === 429) {
                return res.status(429).send({ status: 429, success: false, message: { name: 'Rate Limit Error' } })
            }
            if (context.statusCode === 401){
                return res.status(401).send({ status: 401, success: false, message: { name: 'Authentication Error' } })
            }
            if(!user) {
                // might want to handle this separately? user not found?
                //return next('User not found');
            }

            // this works getting user information
            console.log('user:');
            console.log(user);

            req.user = user;
            next(); // this moves us on to controllerLogin.login

        })(req, res, next);

}, controllerLogin.login);


然后在控制器/登录中

module.exports = {
    login: (req, res) => {
        // remove user logging once this works, don't want to log sensitive info (!)
        console.log('req.user in controller/login:')
        console.log(req.user)

        // user logic here

        res.status(200).json({status: 200, success: true})
  }
}


值得在“自定义回调”部分下查看passportjs docs。该示例不会像您所做的那样传递给另一个函数,但可以帮助您查看另一种方法。

09-25 19:32