【问题标题】:Express Passport Session not working快速护照会话不起作用
【发布时间】:2016-10-27 07:40:05
【问题描述】:

我正在构建一个 Node 应用程序,用户必须在其中注册或登录,然后当他们拖放一些元素(前端全部工作)时,我将他们的操作及其相应的 userId 存储在数据库中。

我的理解是,一旦他们注册/登录,我可以使用 req.user 访问他们的 id 并正确存储他们的操作,但是它不起作用。

这是我的 server.js 文件中处理 Passport 的部分。另外,我使用 Sequelize 作为 ORM,但是没有 req.user 部分,处理数据库的所有内容都可以完美运行。

app.use(cookieParser());
app.use(bodyParser.json());

app.use(passport.initialize());
app.use(passport.session());

/****** Passport functions ******/
passport.serializeUser(function (user, done) {
    console.log('serialized');
    done(null, user.idUser);
});

passport.deserializeUser(function (id, done) {
    console.log("start of deserialize");
    db.user.findOne( { where : { idUser : id } } ).success(function (user) {
        console.log("deserialize");
        console.log(user);
        done(null, user);
    }).error(function (err) {
        done(err, null);
    });
});

//Facebook
passport.use(new FacebookStrategy({
    //Information stored on config/auth.js
    clientID: configAuth.facebookAuth.clientID,
    clientSecret: configAuth.facebookAuth.clientSecret,
    callbackURL: configAuth.facebookAuth.callbackURL,
    profileFields: ['id', 'emails', 'displayName', 'name', 'gender'] 

}, function (accessToken, refreshToken, profile, done) {
    //Using next tick to take advantage of async properties
    process.nextTick(function () {
        db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
            if(err) {
                return done(err);
            } 
            if(user) {
                return done(null, user);
            } else {
                //Create the user
                db.user.create({
                    idUser : profile.id,
                    token : accessToken,
                    nameUser : profile.displayName,
                    email : profile.emails[0].value,
                    sex : profile.gender
                });

                //Find the user (therefore checking if it was indeed created) and return it
                db.user.findOne( { where : { idUser : profile.id } }).then(function (user, err) {
                    if(user) {
                        return done(null, user);
                    } else {
                        return done(err);
                    }
                });
            }
        });
    });
}));

/* FACEBOOK STRATEGY */
// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback//
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']}));
/* FACEBOOK STRATEGY */
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.

    app.get('/auth/facebook/callback',
        passport.authenticate('facebook', { failureRedirect: '/' }),
        function (req, res) {
            // Successful authentication, redirect home.
            res.redirect('../../app.html');
        });


app.get('/', function (req, res) {
    res.redirect('/');
});

app.get('/app', isLoggedIn, function (req, res) {
    res.redirect('app.html');
});

app.post('/meal', function (req, res) {
    //Testing Logs
        /*console.log(req.body.foodId);
        console.log(req.body.quantity);
        console.log(req.body.period);
        console.log(req.body);
        */

    //Check whether or not this is the first food a user drops on the diet
    var dietId = -1;

    db.diet.findOne( { where : { userIdUser : req.user.idUser } } ).then(function (diet, err) {
        if(err) {
            return done(err);
        }
        if(diet) {
            dietId = diet.idDiet;
        } else {
            db.diet.create( { userIdUser : req.user.idUser }).then(function (diet) {
                dietId = diet.idDiet;
            });
        }
    });

    db.meal.create({
        foodId : req.body.foodId,
        quantity : req.body.quantity,
        period : req.body.period
    }).then(function (meal) {
        console.log(meal.mealId);
        res.json({ mealId : meal.mealId});
    });
});

根据我在 Passport 的文档中阅读的内容,每当我使用 req.user 时都应该调用我实现的 deserializeUser 函数,但是,通过我的 console.logs(),我发现登录后调用了 serializeUser,因此它正在存储我的会话,但从未调用 deserializeUser!曾经。

关于如何解决这个问题的任何想法?感谢您的帮助,谢谢!

【问题讨论】:

    标签: node.js session express serialization passport.js


    【解决方案1】:
    1. 确保在设置passport.session中间件之前设置cookieParserexpress-session中间件:
    const cookieParser = require('cookie-parser')
    const session = require('express-session')
    app.use(cookieParser());
    app.use(session({ secret: 'secret' }));
    app.use(passport.initialize());
    app.use(passport.session());
    
    1. 要测试护照会话是否正常工作,请使用:
    console.log(req.session.passport.user)
    

    (例如放在中间件上)

    1. 在我的情况下,我使用的是 LocalStrategy,我想我可以使用简单的用户名和密码作为表单参数来保护和端点,但我虽然护照只会在会话中找不到用户时才使用表单参数。但这是错误的假设。在护照 localStrategy 中,loginprotected endpoint. 应该有单独的端点

    所以请确保您为每个端点使用正确的中间件。就我而言:

    错误

    受保护的端点:

    app.get('/onlyformembers', passport.authenticate('local'), (req, res) => {
      res.send({"res": "private content here!"})
    })
    

    正确

    登录:

    app.post('/login', passport.authenticate('local'), (req, res) => {
      res.send('ok')
    })
    

    受保护的端点:

    var auth = function (req, res, next) {
      if (req.isAuthenticated())
        return next();
      res.status(401).json("not authenticated!");
    }
    
    app.get('/onlyformembers', auth, (req, res) => {
      res.send({"res": "private content here!"})
    })
    

    【讨论】:

      【解决方案2】:

      在调用passport.session() 之前,您需要express session middleware。阅读文档中的passportjs configuration section 了解更多信息。

      【讨论】:

      • 非常感谢!我只是有一个后续问题,什么是“商店”,我为什么需要使用它?我的意思是,为什么我需要另一个模块来存储这些信息?饼干还不够吗?
      • 会话存储是一种在运行快速应用程序的 Nodejs 进程之外存储会话信息的方法。默认情况下,会话中间件使用仅适用于开发目的的内存存储。一旦您进入生产阶段(负载均衡器后面的多个节点进程),默认会话存储将不再为您工作。您可以使用 cookie 来存储会话信息,但请始终记住 cookie 的大小非常有限,并且您可能拥有一些您不想通过网络事件加密的敏感信息。
      猜你喜欢
      • 2019-07-09
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2013-03-15
      • 2013-11-07
      • 2023-01-21
      • 2017-10-17
      相关资源
      最近更新 更多