I have an Angular 7 app which is behind an single sign-on login page. The login page redirects the browser to my app if the login succeeded with the authentication token. In my app module, how can I read the token from the header or get any header info in general?
Asked
Active
Viewed 104 times
0
-
Might be duplicate: https://stackoverflow.com/questions/48184107/read-response-headers-from-api-response-angular-5-typescript – jriver27 Mar 11 '19 at 18:27
1 Answers
1
Try this:
http.get<any>('yourApiUrl', { observe: 'response'})
.subscribe(response => {
// here you can read token
console.log(response.headers.get('X-Token'));
});

Milos Kovacevic
- 861
- 1
- 9
- 20
-
I receive a SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data" because the response is an HTML page and not a json. – DeooK Mar 12 '19 at 08:37
-
More over: With this call, am I not making a new one instead of analyzing that I receipt? – DeooK Mar 12 '19 at 09:50
-
Yeah, this is just a template how can you call your api and read headers from response. I presume that you have token since you are already logged in, right? – Milos Kovacevic Mar 12 '19 at 09:53
-
Yes: there is a login page which I cannot control. If the login succeeded there is a redirect to my home page (i. e. http://myIp:4200/myApp). What I'm trying to understand is how to intercept that request in the app.module constructor and extract the token (plus other infos). The redirect is to an html page (index.html) so I don't know if what I'm trying to do is possible but I'm not finding anything about logging behind an SSO with an external login page. – DeooK Mar 12 '19 at 10:18
-
1Hey, not sure how is token stored after single sign on, check your session storage, maybe is there. – Milos Kovacevic Mar 12 '19 at 13:02
-
1Yeah, by the way use session storage is probably the best practice in my case. – DeooK Mar 12 '19 at 15:28