简体   繁体   中英

How to get JWT token from "Response Header" in angular 6

I am developing an angular 6 application. After entering login credential backend validates the user credentials and sent me a JWT token with headers. Like below-mentioned details. How can I get JWT token in the angular application before redirect to dashboard from login page?

Response Header

Access-Control-Allow-Credentials: true

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

Access-Control-Allow-Origin: *

Cache-Control: private

Content-Length: 138

Content-Type: text/html; charset=utf-8

Date: Tue, 20 Aug 2019 11:29:02 GMT

jwt: eyJhbGciOiJIUz...

Location: http://localhost:4200

I tried using with HttpHeader but getting null value only.

let header = new HttpHeaders();
console.log('read header jwt token', header.get('jwt'));
const token = header.get('jwt');
localStorage.setItem('token', token);

I have same issue during my API call, below code work for me.

http.get<any>('http://localhost:4200')
      .subscribe(resp => {
       console.log('token', header.get('jwt'));
       const token = header.get('jwt');
       localStorage.setItem('token', token);
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM