English 中文(简体)
如何在 no子里设立授权负责人?
原标题:How to set authorization header in node js?

Hello I am working on node application in which I am working on jsonwebtokens,passport-jwt.I created application backend side and working fine on postman but I stuck on front end side. when i send token in headers in postman then token based page open on postman fine but on front side display unauthorized.How can i send token in header so that this page also open on front end side. My code:

app.post("/login", function(req, res) {
  if(req.body.name && req.body.password){
    var name = req.body.name;
    var password = req.body.password;
  }

  var user = users[_.findIndex(users, {name: name})];
  if( ! user ){
    res.status(401).json({message:"no such user found"});
  }

  if(user.password === req.body.password) {
    // from now on we ll identify the user by the id and the id is the only personalized value that goes into our token
    var payload = {id: user.id};
    var token = jwt.sign(payload, jwtOptions.secretOrKey);
    //res.json({message: "ok", token: token});
   res.redirect( /secret )
  } else {
    res.status(401).json({message:"passwords did not match"});
  }
});

app.get("/secret", passport.authenticate( jwt , { session: false }), function(req, res){
    res.json("Success! You can not see this without a token");

  });

哪里是错了?

问题回答

页: 1 在一届会议上,你可以拯救他们。 储存供今后使用......

类似情况

 if(user.password === req.body.password) {
  ....
        var payload = {id: user.id};
        var token = jwt.sign(payload, jwtOptions.secretOrKey);
        req.session.token = token ;
    }

}

利用本届会议更新会议 客户方面的储存

https://stormpath.com/blog/ where-to-store-your-jwts-cookies-vs-html5-web-storage”rel=“nofollow noreleaseer”>here是条,这是你需要把 lo留在原木中......

also you need to destroy the cookies on Logout

如果你觉醒,你可以寄送:

let headers = new Headers({ 
 Content-Type :  application/json ,
 Authorization :  Bearer   + this.token 
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签