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.

Authorization Header not added within Ionic 3

See original GitHub issue

I am trying to implement JWT in my Ionic 3 app, and have been using angular2-jwt successfully before (without HttpInterceptor). But the master v1.0 branch doesn’t seem to working, with all modules fully updated.

When doing as suggested in the Configuration for Ionic 2, there are no "Authorization: " headers sent.

My app.module.ts has the following:

...
import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { HttpClientModule } from '@angular/common/http';
...
const storage = new Storage({
                              name: 'myapp',
                              driverOrder: ['sqlite', 'indexeddb', 'websql'],
                            });
export function jwtOptionsFactory() {
  return {
    tokenGetter: () => {
      return storage.get('id_token');
    }
  }
}

@NgModule({
  declarations: [MyApp, AnnouncementModalPage],
  imports: [
   HttpClientModule,
 JwtModule.forRoot({
                        jwtOptionsProvider: {
                          provide: JWT_OPTIONS,
                          useFactory: jwtOptionsFactory
                        }
                      }),...

Doing requests

Code for doing actual requests:

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { JwtHelperService } from '@auth0/angular-jwt'
...

@Injectable()
export class InspirationProvider {
  constructor(public http: HttpClient,
              private jwtHelper: JwtHelperService..) {
  }

  private fetchData(): Promise<Data[]> {

    let token: string = this.jwtHelper.tokenGetter();
    console.log("This outputs a Promise having the token", token);


    return new Promise((resolve, reject) => {
      this.http.get<Data[]>(INSPIRATION_URL)
        .toPromise()
        .then(data => {
          resolve(data);
        })
        .catch((err) => {
          // something bad happened
          reject(err);
        })
    });
  }
}

My ionic info

cli packages: (/Users/rogier/.nvm/versions/node/v6.9.3/lib/node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

global packages:

    cordova (Cordova CLI) : 7.0.1

local packages:

    @ionic/app-scripts : 3.1.2
    Cordova Platforms  : android 6.2.3 browser 4.1.0 ios 4.4.0
    Ionic Framework    : ionic-angular 3.9.2

System:

    Android SDK Tools : 25.2.2
    ios-deploy        : 1.9.2
    Node              : v6.9.3
    npm               : 3.10.10
    OS                : macOS Sierra
    Xcode             : Xcode 9.1 Build version 9B55

Problem: While looking at the request being made, there is no “authorization” header.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

23reactions
tylervzcommented, Dec 1, 2017

I got it working with my app.module.ts looking like this:

...
import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { HttpClientModule } from '@angular/common/http';
import { IonicStorageModule, Storage } from '@ionic/storage';
...
export function jwtOptionsFactory(storage) {
  return {
    tokenGetter: () => {
      return storage.get('id_token');
    },
    whitelistedDomains: ['localhost:8080']
  }
}

@NgModule({
  declarations: [MyApp, AnnouncementModalPage],
  imports: [
    HttpClientModule,
    IonicStorageModule.forRoot(),
    JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [Storage]
      }
    }),
    ...
1reaction
EcoleDuNumeriquecommented, Nov 26, 2017

Hi,

I’ve same problem, with whitelistedDomains…

export function jwtOptionsFactory() { return { tokenGetter: () => { return storage.get('access_token'); }, config: { whitelistedDomains: ['localhost:3001', 'notes.api', 'localhost:8100', '[::1]:80', 'localhost', 'localhost:8200'], } } }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding "Authorization" header in get request - Ionic Forum
I'm developing Ionic application with Spring framework back-end. Application is simple, check specified URI to check user is authenticated ...
Read more >
Ionic 3.5.2 Authorization header missing in http request
I double check with the fiddler, I found a difference, that is the header Authorization is missing, it is not sent along with...
Read more >
Developers - Authorization Header not added within Ionic 3 -
I am trying to implement JWT in my Ionic 3 app, and have been using angular2-jwt successfully before (without HttpInterceptor ). But the...
Read more >
Authorisation Headers with Ionic using HTTP Interceptor and ...
In this Quick Win we will add a custom HTTP interceptor which will intercept all outgoing HTTP calls that our Ionic app makes....
Read more >
Access-Control-Allow-Headers - HTTP - MDN Web Docs
In requests with credentials, it is treated as the literal header name " * " without special semantics. Note that the Authorization header...
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