question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

1.1.0 doesn't add the authorisation bearer to the header

See original GitHub issue

Upgraded from beta9 to 1.1.0 because #477 is fixed but, now something else seems to be broken.

I can’t see the authorisation Bearer part anywhere in the header. I tried adding localhost to the whitelist, but that didn’t help as well. Is something else changed?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:39 (7 by maintainers)

github_iconTop GitHub Comments

42reactions
crookseycommented, Mar 23, 2018

Seeing as how this library can sometimes be buggy, I decided to write my own HTTP_INTERCEPTOR, very easy, simply replace ‘rawJWT’ with the location of your JWT from local storage.:

import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class JwtHttpInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token = localStorage.getItem('rawJWT');
      let clone: HttpRequest<any>;
      if (token) {
        clone = request.clone({
          setHeaders: {
            Accept: `application/json`,
            'Content-Type': `application/json`,
            Authorization: `Bearer ${token}`
          }
        });
      } else {
        clone = request.clone({
          setHeaders: {
            Accept: `application/json`,
            'Content-Type': `application/json`
          }
        });
      }
      return next.handle(clone);
  }
}

Then in app.module.ts (note you have to import the above first as normal)

providers: [
    { provide: HTTP_INTERCEPTORS, useClass: JwtHttpInterceptor, multi: true },

This library is great for decoding JWT’s but this is not the first time an update has broken the code, so with this easy code I can control how and where my JWT gets sent

10reactions
1619digitalcommented, Mar 15, 2018

Repeating from #481 - If you leave the whitelist empty the isWhitelistedDomain method will always return false. It will never match anything, and hence never send the authorization headers. (This is contrary to the documentation, which implies that having an empty whitelist will match local domain requests.) Furthermore, adding localhost won’t work, because the domain for a domain-less request is null.

Workaround - If you use domain-less routes, the workaround is to add the null domain to the whitelist, which you can do through a RegExp. In other words, do this:

whitelistedDomains: [ /^null$/ ]

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maven not adding "Authorization" header - Stack Overflow
I'm trying to deploy artifacts to a protected by Basic Auth repository. I specify <distributionManagement> <repository> <id> ...
Read more >
HTTP error 401.1 with pre-authentication headers - Internet ...
An unexpected 401.1 status is returned when you use Pre-Authentication headers with Internet Explorer and Internet Information Services ...
Read more >
Generating and using app-only Bearer Tokens | Docs
A bearer token allows developers to have a more secure point of entry for using the Twitter APIs, and are one of the...
Read more >
OAuth 2.0 token endpoint - Connect2id
Authorisation code -- the code obtained from the authorisation endpoint which the server uses to look up the permission or consent given by...
Read more >
WWW-Authenticate - HTTP - MDN Web Docs
The HTTP WWW-Authenticate response header defines the HTTP authentication methods ("challenges") that might be used to gain access to a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found