I have the following problem to try to use a library-based service auth0 / angular-jwt The service is as follows:
import {Injectable} from '@angular/core';
import {JwtHelperService} from '@auth0/angular-jwt';
@Injectable({
providedIn: 'root'
})
export class JwtHandlerService {
public _jwtHelper: any;
constructor() {
this._jwtHelper = new JwtHelperService();
}
/**
* Get the token expiration date
* @param {string} token [ JWT Token ]
* @return [ Expiration date decoded ]
*/
public getTokenExpirationDate(token: string) {
return this._jwtHelper.getTokenExpirationDate(token);
}
/**
* Check if the token has expired
* @param {string} token [ JWT Token ]
* @return [ True or false ]
*/
public isTokenExpired(token: string): boolean {
return this._jwtHelper.isTokenExpired(token);
}
/**
* Get the user data from a success response
* @return [ Decoded token ]
*/
public getUserData(success: any) {
return this.decodeToken(success.TokenInfo.IDToken); // TODO: Need to be More clean - @vdsancheza
}
/**
* Get user data from the openid
* @param {any} openid [description]
* @return [description]
*/
public getUserDataFromOpenid(openid: any) {
return this.decodeToken(openid);
}
/**
* Decode token function
* @param {string} token [ JWT Token ]
* @return [ Decoded token ]
*/
private decodeToken(token: string) {
return this._jwtHelper.decodeToken(token);
}
}
More specifically I am using the isTokenExpired method to verify if the token I sent as a parameter is valid or not.
The problem I have is that when using it on an interceptor that has the following form
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import {JwtHandlerService, SessionStorageService} from '../services';
import {LoginService} from '@app/modules/auth/services/login/login.service';
@Injectable({
providedIn: 'root'
})
export class TokenInterceptor implements HttpInterceptor {
constructor(private _sessionSS: SessionStorageService, private _jwtH: JwtHandlerService, private _loginS: LoginService) {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// request = request.clone({
// setHeaders: {
// Authorization: 'Bearer ${this.auth.getToken()}'
// }
// });
console.log('Hola soy victor interceptando');
const refreshExist = this._sessionSS.validate('refresh');
const accessExist = this._sessionSS.validate('access');
const refresh = this._sessionSS.getItem('refresh');
const access = this._sessionSS.getItem('access');
console.log('First Verif: ', refreshExist && accessExist);
if ( refreshExist && accessExist ) {
console.log('Entre');
console.log(this._jwtH.isTokenExpired(refresh), 'hola');
if (this._jwtH.isTokenExpired(access)) {
console.log('pase al servicio');
this._loginS.loadRefresh(refresh, access)
.subscribe(success => {
console.log('success:', success);
}, error => {
});
}
}
return next.handle(request);
}
}
Makes my application hang right on the line where I consult the isTokenExpered method of the aforementioned service. I tried:
I do not see another way or other ways to try, but I am not able to verify that token with that method because it simply does not return anything, it is worth noting that I have also verified the official documentation auth0 / angular2-jwt
If you have any idea how to address this problem, I am open to provide the necessary information, as well as update the question with what works for me so far.