Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(authentication): use jwt-decode to decode token
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Jun 1, 2016
1 parent fdbb918 commit f5056ce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 5 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ System.config({
"aurelia-router": "npm:aurelia-router@1.0.0-beta.1.2.3",
"extend": "npm:extend@3.0.0",
"fetch": "github:github/fetch@0.11.1",
"jwt-decode": "npm:jwt-decode@2.0.1",
"npm:aurelia-api@3.0.0-rc2": {
"aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-beta.1.2.3",
"aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0-beta.1.2.5",
Expand Down Expand Up @@ -52,6 +53,10 @@ System.config({
"aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.1",
"aurelia-path": "npm:aurelia-path@1.0.0-beta.1.2.2",
"aurelia-route-recognizer": "npm:aurelia-route-recognizer@1.0.0-beta.1.2.1"
},
"npm:jwt-decode@2.0.1": {
"fs": "github:jspm/nodelibs-fs@0.1.2",
"systemjs-json": "github:systemjs/plugin-json@0.1.2"
}
}
});
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"aurelia-pal": "^1.0.0-beta.1.2.2",
"aurelia-path": "^1.0.0-beta.1.2.2",
"aurelia-router": "^1.0.0-beta.1.2.3",
"extend": "^3.0.0"
"extend": "^3.0.0",
"jwt-decode": "^2.0.1"
},
"peerDependencies": {
"aurelia-api": "^3.0.0-rc2",
Expand All @@ -49,7 +50,8 @@
"aurelia-pal": "^1.0.0-beta.1.2.2",
"aurelia-path": "^1.0.0-beta.1.2.2",
"aurelia-router": "^1.0.0-beta.1.2.3",
"extend": "^3.0.0"
"extend": "^3.0.0",
"jwt-decode": "^2.0.1"
},
"devDependencies": {
"aurelia-pal-browser": "^1.0.0-beta.2.0.1",
Expand All @@ -66,7 +68,8 @@
"aurelia-pal": "^1.0.0-beta.1.2.2",
"aurelia-path": "^1.0.0-beta.1.2.2",
"aurelia-router": "^1.0.0-beta.1.2.3",
"extend": "^3.0.0"
"extend": "^3.0.0",
"jwt-decode": "^2.0.1"
},
"devDependencies": {
"aurelia-tools": "^0.1.20",
Expand Down
17 changes: 6 additions & 11 deletions src/authentication.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {PLATFORM} from 'aurelia-pal';
import {inject} from 'aurelia-dependency-injection';
import {deprecated} from 'aurelia-metadata';
import jwtDecode from 'jwt-decode';
import * as LogManager from 'aurelia-logging';

import {BaseConfig} from './baseConfig';
Expand Down Expand Up @@ -146,19 +147,13 @@ export class Authentication {
}
}

let payload = null;
this.payload = null;

if (this.accessToken && this.accessToken.split('.').length === 3) {
try {
const base64 = this.accessToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
payload = JSON.parse(decodeURIComponent(escape(atob(base64))));
} catch (e) {
payload = null;
}
}
try {
this.payload = this.accessToken ? jwtDecode(this.accessToken) : null;
} catch (_) {}

this.payload = payload;
this.exp = payload ? parseInt(payload.exp, 10) : NaN;
this.exp = this.payload ? parseInt(this.payload.exp, 10) : NaN;

this.hasDataStored = true;

Expand Down

0 comments on commit f5056ce

Please sign in to comment.