Sails.js Sessions - 记住我的功能 [英] sails.js Sessions - rememberme functionality

查看:64
本文介绍了Sails.js Sessions - 记住我的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 sails 实现记忆功能.我希望会话持续到浏览器关闭.但是,如果在用户登录时勾选了记住我复选框,我希望会话持续 30 天.我从这里使用了记住我的护照策略:https://github.com/jaredhanson/passport-remember-me但它最终坐在风帆会话的顶部,并且被称为第一个的最终取代了另一个.

I have been trying to implement rememberme functionality using sails. I want a session to persist until the browser is closed. However, if a rememberme checkbox is ticked when the user logins in I want the session to persist for 30 days. I used the remember me passport strategy from here: https://github.com/jaredhanson/passport-remember-me but it ends up sitting on top of the sails session and the one called first ends up superseding the other.

推荐答案

您可以在调用登录函数之前设置 cookie 年龄.

You can set the cookie age just before calling the login function.

我是在我的登录控制器中完成的 ->passport.callback.

I did it in my login controller -> passport.callback.

passport.callback(req, res, function (err, user, challenges, statuses) {
        ...

        if (req.param('remember', false) == true) {
            req.session.cookie.maxAge = 1000 * 60 * 60 * 24 * 30;
        }
        req.login(user, function (err) {
        ...
        }
} 

这感觉不太对,如果您在登录时发送一些其他 cookie,也会影响它们的生命周期.

This doesn't really feel right and, if you are sending some other cookies when logging in, it will affect their lifetime as well.

但它有效,我选择了它,因为查找与风帆相关的文档就像挖油一样.

But it works and I went with it since finding documentation for sails-related stuff is like digging oil.

另外,我注意到 Passport 没有正确销毁会话,必须通过在注销控制器中调用 req.session.destroy(); 手动完成.

Also, I noticed that Passport was not destroying sessions properly upon and had to do it manually by calling req.session.destroy(); in the logout controller.

这篇关于Sails.js Sessions - 记住我的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆