angular2-jwt icon indicating copy to clipboard operation
angular2-jwt copied to clipboard

Authorization header not being sent - Angular 5

Open ghost opened this issue 8 years ago • 19 comments

I am having this same issue consistently in a prod environment using Angular 5 but It works completely fine locally! I installed using this command: npm install @auth0/angular-jwt

For a tl;dr - I was previously using Angular 4.4.6 with angular2-jwt, but now that http is deprecated, I am using HttpClient with this newer version of angular-jwt.

Here is what I have done: My app.module.ts looks as follows (showing both the inline function and a custom factory syntax that I tried)

// app.module.ts
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { TokenService } from './_services/token.service';

export function jwtOptionsFactory(tokenService) {
  return {
    tokenGetter: () => {
      return tokenService.get();
    },
    whitelistedDomains: ['budgetivity.com', 'budgetivity.local']
  };
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
	...
    HttpClientModule,
    JwtModule.forRoot({
      config: {
        tokenGetter: () => {
          return sessionStorage.getItem('token');
        },
        whitelistedDomains: ['budgetivity.com', 'budgetivity.local']
      }
      // jwtOptionsProvider: {
      //   provide: JWT_OPTIONS,
      //   useFactory: jwtOptionsFactory,
      //   deps: [TokenService]
      // }
    }),

I am using full IIS locally with my host file pointing to 127.0.0.1 for budgetivity.local

When I run locally, I am building for dev and I use the ng-build command and my environment.ts looks as follows:

export const environment = {
	...
  baseUrl: 'http://budgetivity.local',
	...
};

When I build for prod I use ng build --env=prod and my environment.prod.ts looks as follows:

export const environment = {
	...
  baseUrl: 'https://www.budgetivity.com',
	...
};

But - when running a ng-build --prod command I get an error if I am using the function syntax instead of the custom service syntax. "Error encountered resolving symbol values statically. Function calls are not supported." So when using the ng-build --prod command I must switch the code to only use the custom service syntax.

When running locally, my requests are sucessfully sent to my local API and have an appropriate Authorization: Bearer XXXX (token) When built for and deployed to Prod, the requests do not have an Authorization header at all.

I have even built for dev but used the Prod baseUrl - when that is deployed to Prod that also does not send the Authorization header in any of the requests.

For sake of completeness here is my tokenService and a method that calls my API:

// token.service.ts
import { Injectable } from '@angular/core';

@Injectable()
export class TokenService {

  public get(): string {
    return sessionStorage.getItem('token');
  }

  public set(token: string): void {
    sessionStorage.setItem('token', token);
  }

  public deleteToken(): void {
    sessionStorage.removeItem('token');
  }
}
// session.service.ts
import { environment } from '../../environments/environment';
...
  public isSessionValid(): Observable<any> {
    return this.http.get(`${environment.baseUrl}/api/v1/sessions`)
      .catch((error: Response) => {
        return Observable.throw(error);
      });
  }

...

Here is what my local call looks like: image

and here is what it looks like on Prod: image

ghost avatar Dec 10 '17 03:12 ghost

For what it's worth, I ended up ditching this library altogether. For determining if a user is still logged in instead of decoding the token on the front end I send a request to my API to a SessionsController that is authorized and simply returns Ok if the authorization attribute logic passes - determines that the token exists in the DB, hasn't expired and hasn't been administratively deactivated. This is much safer and gives the server control of determining if the session is actually valid or not. If it's not valid it returns a 401 and my guard informs the user and signs them out.

// secure.guard.ts
...
@Injectable()
export class SecureGuard implements CanActivate {

  constructor(private router: Router,
              private alertService: AlertService,
              private sessionService: SessionService) { }

  public canActivate() {
    return this.serverSessionIsValid();
  }

  public serverSessionIsValid(): Observable<boolean> {
    return Observable.create((observer) => {
      this.sessionService.isSessionValid().subscribe(
        () => {
          observer.next(true);
          observer.complete();
        },
        () => {
          observer.next(false);
          observer.complete();
          this.sessionService.clearSession();
          this.router.navigate(['/sign-in']);
          this.alertService.alert(AlertType.error, 'Your session has expired, you will need to log in.');
        });
    });
  }
...

For adding the authorization header, I just add it to the necessary calls like so:

// session.service.ts
...
  public get authHeader(): string {
    return `Bearer ${sessionStorage.getItem('token')}`;
  }
...
  public isSessionValid(): Observable<any> {
    return this.http.get(`${environment.baseUrl}/api/v1/sessions`, {
      headers: new HttpHeaders().set('Authorization', this.authHeader)
    })
    .catch((error: Response) => {
      return Observable.throw(error);
    });
  }
...

ghost avatar Dec 10 '17 14:12 ghost

I am running into same problem.

mamdi52 avatar Jan 31 '18 04:01 mamdi52

Me too 😢

eddiejaoude avatar Feb 01 '18 19:02 eddiejaoude

Me three 😭

samsop avatar May 04 '18 07:05 samsop

+1

nickob2000 avatar May 04 '18 14:05 nickob2000

Same here

abalone49 avatar May 22 '18 21:05 abalone49

+1

G-Nantel avatar May 30 '18 19:05 G-Nantel

Hey! I had the same issue where token wasn't being passed to the server on production. But my issue got resolved by updating the whitelistedDomains array.

JwtModule.forRoot({
      config: {
        tokenGetter: tokenGetter,
        whitelistedDomains: ['localhost:8000','prod-1.app.io','api.prod-2.app.io'],
        authScheme: 'JWT '
      }
    })

hemangsk avatar Jun 12 '18 09:06 hemangsk

I'm also getting same problem

ahsishphp avatar Jun 27 '18 13:06 ahsishphp

I'm also experiencing the same issue

lucasalvarezlacasa avatar Jun 27 '18 14:06 lucasalvarezlacasa

I am also having the same issue on production with Angular 5. Any solutions?

gitSambhal avatar Jul 13 '18 12:07 gitSambhal

Solutions that worked for me:

  1. Use interceptor https://stackoverflow.com/a/45363296
  2. Use HttpClient module instead of Http

gitSambhal avatar Jul 19 '18 09:07 gitSambhal

HT to @sambhalgithub. If you're using Angular 6 you have to switch from Http to HttpClient or the authorization header is not appended.

Auth0, it would help if you updated the Readme.MD to specify this.

jelling avatar Sep 11 '18 15:09 jelling

For anyone still getting this issue when building for production: I was using a regex replace to remove the protocol from the environment variable. After removing the regex the problem vanished.

   // apiEndpoint:https://foo.com
   // whitelistedDomains: [ environment.apiEndpoint.replace(/^https?\:\/\//i, '') ] 

   // apiHost: foo.com
   whitelistedDomains: [ environment.apiHost ]

ghry5 avatar Sep 27 '18 08:09 ghry5

Just configure your app module by this way:

...
import {JwtInterceptor, JwtModule} from '@auth0/angular-jwt';
...

@NgModule({
  imports: [
    ...
    JwtModule.forRoot({
      config: {
        tokenGetter: () => localStorage.getItem('varName'),
        whitelistedDomains: ['foo.bar']
      }
    })
  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true}
    ...
  ],
  ...
})

oyepez003 avatar Feb 14 '19 04:02 oyepez003

Hey! I had the same issue where token wasn't being passed to the server on production. But my issue got resolved by updating the whitelistedDomains array.

JwtModule.forRoot({
      config: {
        tokenGetter: tokenGetter,
        whitelistedDomains: ['localhost:8000','prod-1.app.io','api.prod-2.app.io'],
        authScheme: 'JWT '
      }
    })

Thaaanks @hemangsk whitelistedDomains should be without "http://" or "https://", this was my issue

youssef-ali avatar Jun 13 '19 07:06 youssef-ali

Heyy @woloski i am still getting this error while ng build --aot ERROR in Error during template compile of 'AppModule' Function expressions are not supported in decorators in 'ɵ0' 'ɵ0' contains the error at src\app\app.module.ts(228,22) Consider changing the function expression into an exported function at JwtModule.forRoot({
config: { tokenGetter: () => localStorage.getItem('token'), whitelistedDomains: ['localhost:4200','domain','domain'], // authScheme: 'JWT ' } }),

please help me

riyaagarwal2111 avatar Jun 18 '19 04:06 riyaagarwal2111

Thaaanks @youssef-ali whitelistedDomains should be without "http://" or "https://", this was my issue

evthulasiram avatar Jul 12 '19 10:07 evthulasiram

i am using Angular17

login(username: String , password: String): any { const headers = new HttpHeaders().set('Content-Type', 'application/json'); const body = { username, password }; console.log(headers); console.log(body); console.log("Inside Login"); return this.http.post(BASIC_URL + "authenticate", body, { headers, observe: 'response' }).pipe( map((res) => { const authToken = res.headers.get('Authorization'); const jwtToken = authToken ? authToken.replace('Bearer ', '') : null; const user = res.body; console.log("Inside login return statement"); console.log("AuthToken: "+authToken); console.log("Jwt Token: "+jwtToken); console.log("User: "+user); if (jwtToken && user) { this.userStorageService.saveToken(jwtToken); this.userStorageService.saveUser(user); return true; } return false; }) );

when i run this code i am getting
auth token : null
Jwt Token : null
user: [object,object]

everything is fine on backend somebody help me

Vinayshetty55 avatar May 01 '24 11:05 Vinayshetty55