Auth0 JWT - AngularBuildUp/angular-microworkshops-authorization GitHub Wiki

An Angular library that helps us work with JWTs. (https://github.com/auth0/angular2-jwt)

Key features

  • Decode a JWT from your AngularJS app
  • Check the expiration date of the JWT
  • Automatically send the JWT in every request made to the server
  • Manage the user's authentication state with authManager

The JwtHelper service provides such helper methods. Very helpful is the decoding of the token, the .decodeToken method, which can decode a string JWT token into an object (like in jwt.io). Decoding a token means we can read the payload and extract its claims if any, so we can apply authorization.

import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}

ngOnInit() {
    console.log(this.jwtHelper.decodeToken(token)); // token
}

We can also check the expiration date and determine if the token has expired.

import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}

ngOnInit() {
    console.log(this.jwtHelper.getTokenExpirationDate()); // date
}
import { JwtHelperService } from '@auth0/angular-jwt';
// ...
constructor(public jwtHelper: JwtHelperService) {}

ngOnInit() {
    console.log(this.jwtHelper.isTokenExpired()); // true or false
}