sails.js Sessions - rememberme functionality

 ̄綄美尐妖づ 提交于 2019-12-24 00:43:18

问题


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.


回答1:


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

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) {
        ...
        }
} 

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.

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.




回答2:


Basically you can set the cookie yourself and set the expires manually

    res.cookie('userSession', {
        items: [1, 2, 3]
    }, {
        expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000),
        httpOnly: true
    })


来源:https://stackoverflow.com/questions/29634824/sails-js-sessions-rememberme-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!