daily pastebin goal
89%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2.  
  3. @Injectable()
  4. export class JWTUtil {
  5.  
  6.     constructor() { }
  7.  
  8.     decodeToken(accessToken: string) {
  9.         let base64Url = accessToken.split('.')[1];
  10.         let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
  11.         let JwtDecode = JSON.parse(window.atob(base64));
  12.         console.info("Decoded Token: ", JwtDecode);
  13.         return JwtDecode
  14.     }
  15.  
  16.     expirationTime(accessToken: string): number {
  17.         let parseJwt = this.decodeToken(accessToken);
  18.         let expTime = parseJwt.exp;
  19.         return expTime
  20.     }
  21.  
  22.     isExpired(accessToken: string): Boolean {
  23.         let expTime = this.expirationTime(accessToken);
  24.         console.log("Expiration time: ", expTime, new Date(expTime))
  25.         let currentTimeInMilli = new Date().getTime();
  26.         console.log("Current time: ", currentTimeInMilli, new Date(currentTimeInMilli))
  27.         if (expTime < currentTimeInMilli) {
  28.             console.warn("Token Expired!")
  29.             return true
  30.         }
  31.         return false
  32.     }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top