Auth0 Home Blog Docs

Auth http: No JWT present or has expired

jwt
auth
ionic2

#1

I know there is a lot on different forums on this issue. I looked here, on ionic forum and on stackoverflow.

When I’m trying to make an api call I get the response 401 (Unauthorized): No JWT present or has expired.

in my app.module.ts, above @ngmodule I have an getAuthHttp function:

export function getAuthHttp(http) {
    return new AuthHttp(new AuthConfig({
        headerPrefix: 'bearer',
        noJwtError: true,
        globalHeaders: {'Accept': 'application/json'}],
        tokenGetter: (() => JSON.parse(window.localStorage.getItem('id_token'))),
    }), http);
}

(I tried to fix tokengetter by getting token from localstorage instead of creating new storage as is suggested in some official documentation)

this is my providers array:

providers: 
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    {provide: AuthHttp, useFactory: getAuthHttp, deps: [Http] },
    AUTH_PROVIDERS,
    ChallengeService, SettingsService, UserdataService, AuthService
  ]

and of course I have an authservice file which handles login and logout and probaly more which I don’t use at the moment.

My service which calls the api:

import { Injectable} from '@angular/core';
import { AuthService} from "./auth.service";
import { AuthHttp } from "angular2-jwt";
import 'rxjs/Rx';

@Injectable()
export class MessageService{

    http: any;
    messages: any;
    authService: any;
    
    constructor(authService:AuthService, private authHttp:AuthHttp){
        this.storage = storage;
        this.authService = authService;
        this.authHttp = authHttp;
    }

    getMessage(level, location)
    {
        this.messages=  this.authHttp.get('http://10.0.2.2:8080/api/message/1');
        return this.messages;
    }
};

Now this goes wrong, obviously.

I 'm pretty sure i’m forgetting something but i can’t pinpoint it and after days of getting closer now i’m totally stuck. Who can help me fix this? I would also be okay with just using angular http but I cannot get it working with sending the headers myself as well.


#2