Access value in HTTP response

Asked

Viewed 57 times

0

Hello!!

I’m implementing a login method. When the user logs in, the backend generates a JWT and sends the token to be stored in the HTML localStorage. I never used TS/Angular 4, Angularjs had no problem with that, see my function, can you tell me the prq of not working? The console.log("response: ", response.body.token) is returning null.

login() {
  this.auth.authenticate(this.creds)
    .subscribe(response => {
        console.log("response: ", response.body.token)
        this.auth.successfulLogin(response.headers.get('Authorization'))
        this.navCtrl.setRoot('DetalhesViagemPage')
      },
      error => {})
}

Return of console.log("response: ", response.body):

response:  {"nome":"Frederico","_id":"5b21a4332a5e3333cc64a12f","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyIkX18iOnsic3RyaWN0TW9kZSI6dHJ1ZSwic2VsZWN0"}
  • have tried response.headers('Authorization')?

  • I’m sorry, but I don’t understand what’s not working.Is it saving in localStorage? If what you need is to return the token passes what is in the body. this.auth.successfulLogin(Response.body.token);

  • @Ricardopunctual How can I pass the token through the header, using express/Node? response.headers.get('Authorization') returns null, I believe prq is receiving the token by body.

  • @Lucasbrogni O console.log("response: ", response.body.token) is returning null. However, as you can see, in the return of Response.body there is the key token :/

  • and the remaining header comes filled? if inspect response.headers have something? to set the token in nodejs it’s that simple: response.set('token', 'valor'). A doubt is a request in the same domain or cross-Domain ?

  • If you are making a request for a different domain you need to inform that this header can be exposed (the token), like this: response.header('Access-Control-Expose-Headers', 'token')

  • @Ricardopunctual backend is localhost, same domain :/ I am using CORS too, is released cross-Omain. I think yesterday I had tested with Sponse.set, I will try again here.

  • that, put there the result if it worked

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.