用的nodejs/express ,寫用戶注冊、登陸的功能。
要求:寫一個中間件,用來進行用戶驗證。中間件的功能是驗證JWT是否為空,是否過期,是否被篡改。
我的問題是:首次log in時res.header里產生了自定義的JWT(我起名叫 x-auth-token),而這個JWT是怎樣傳到req的header中去呢?
我用的是 jsonwebtoken npm.
在網上找了個類似的:
var response = require('./response')
var common = require('./common')
module.exports = function(req, res, next){
var token = req.body.token || req.query.token || req.headers['token']
if (token){
common.verifytoken(token).then((data)=>{
if(data){
req.decoded=data
next()
}
else{
res.send(response.out("無效的token,請重新登錄"))
}
})
}
else{
res.send(response.err("沒有傳token,請先登錄"))
}
}出處:https://www.jianshu.com/p/9e76c01ebaf3
但是請看第5行, 他的 req.body.token是怎么傳進去的呢?
他只說了:“把data放到token里面,回傳給用戶,下次需要驗證的時候客戶端帶上需要帶上token值進行傳輸。”
但是怎么“帶上需要帶上token值”呢?
請大神們幫忙!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/188520.html
標籤:JavaScript
