2016-08-24 110 views
0

MeanJS,在用户的loggedIn,我们刷新页面,在core.server.controller.js将发送用户凭据到index.html登录MeanJS API

exports.renderIndex = function (req, res) { 
    res.render('modules/core/server/views/index', { 
    user: req.user || null 
    }); 
}; 

QUES 1:为什么服务器知道是谁登录用户?即使我们已经关闭浏览器并重新打开它。

QUES 2

Lets't拨打以上的应用程序的域是本地主机:8000。

我有一个单独的网站,只有前端代码(angular)没有后端。我们称它为localhost:3000。我可以从localhost:8000调用API并显示数据。

我也可以通过调用localhost:8000/api/auth/signin API来登录,但是在我刷新它之后,它不会将我识别为signedin用户,因为我没有服务器为我提供index.html localhost:3000 。

使本地主机:8000登录的任何伎俩工程?

回答

1

关于问题1,这是因为配置了express-session的方式而发生的。如果你关闭浏览器cookie的会话将持续,除非你改变config/env/default.js定义的选项,分别是:

// Session Cookie settings 
sessionCookie: { 
    // session expiration is set by default to 24 hours 
    maxAge: 24 * (60 * 60 * 1000), 
    // httpOnly flag makes sure the cookie is only accessed 
    // through the HTTP protocol and not JS/browser 
    httpOnly: true, 
    // secure cookie should be turned to true to provide additional 
    // layer of security so that the cookie is set only when working 
    // in HTTPS mode. 
    secure: false 
}, 

根据express-sessiondocs可以使用expiremaxAge来控制行为。如果maxAgeexpire都是未设置大多数客户会认为这是一个“非持久性cookie”,并且会在退出Web浏览器应用程序等情况下将其删除。在MEAN.js maxAge已设置,这就是为什么用户保持登录即使浏览器关闭。但是,24小时后用户需要重新登录。

关于问题2,我从来没有尝试过这样的事情,但我想答案可能是在express-sessiondocsdomainpathsameSite性能。看一看,看看有什么东西根据你的需求。