2016-08-24 40 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性能。看一看,看看有什麼東西根據你的需求。