[英]User authentification in express.js / Node.Js
我在運行在Node.Js / Express.Js上的應用程序中的用戶身份驗證有問題
基本上,當用戶登錄主界面(html)時,會顯示一個圖,該圖是通過API路徑(/ api / data / json)獲取的,該路徑也需要通過以下方式進行身份驗證
exports.auth = express.basicAuth(User.authenticate);
在routes / api.js中
我嘗試“告訴” routes / api.js,方法是通過將其傳遞給res.locals.user
參數之一來授權用戶,但這無濟於事...
所以我的問題是:Node.Js中是否可以將用戶的登錄詳細信息存儲在會話變量中(例如您可以在PHP中進行存儲),以便在需要授權時將其傳遞給所有組件,以避免用戶擁有在他們訪問應用程序的另一部分時重新授權?
謝謝!
身份驗證成功后,basicAuth中間件將使用用戶名填充req.user
。 要檢查用戶是否在隨后出現的任何中間件(例如,在routes/api.js
)中通過了身份驗證, routes/api.js
檢查req.user
是否已定義:
if (req.user) {
// User is already auth'ed; provide secret data
} else {
// Proceed to next middleware to provide a standard error message
next();
}
或者,您也可以使用res.redirect('/goToOtherpage')
重定向到其他路由,或者可以使用res.send(401)
響應HTTP錯誤,以向呼叫者發出Unauthorized
的信號。
不知道這是否有用,但是您可以加入HTML5會話存儲。 這是一篇涉及更多內容的文章。
http://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/
這是有關如何使用它的文章中的示例。
//save a value
sessionStorage.setItem("name", "Nicholas");
//retrieve item
var name = sessionStorage.getItem("name");
//get the key name for the first item
var key = sessionStorage.key(0);
//remove the key
sessionStorage.removeItem(key);
//check how many key-value pairs are present
var count = sessionStorage.length;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.