[ADF-4665] [ADF] - Application is refreshed when you have two instances of application opened (#4849)

* refactoring getValueFromToken and fix user token refresh

* refactoring getValueFromToken and fix user token refresh

* refactoring getValueFromToken and fix user token refres

* fix unit test
This commit is contained in:
Eugenio Romano
2019-06-14 16:02:12 +01:00
committed by GitHub
parent 4733bc7d3b
commit f47cebc0a4
11 changed files with 94 additions and 93 deletions

View File

@@ -22,7 +22,15 @@ import { Injectable } from '@angular/core';
})
export class JwtHelperService {
constructor() {}
static USER_NAME = 'name';
static FAMILY_NAME = 'family_name';
static GIVEN_NAME = 'given_name';
static USER_EMAIL = 'email';
static USER_ACCESS_TOKEN = 'access_token';
static USER_PREFERRED_USERNAME = 'preferred_username';
constructor() {
}
/**
* Decodes a JSON web token into a JS object.
@@ -64,4 +72,29 @@ export class JwtHelperService {
}
return decodeURIComponent(escape(window.atob(output)));
}
/**
* Gets a named value from the user access token.
* @param key Key name of the field to retrieve
* @returns Value from the token
*/
getValueFromLocalAccessToken<T>(key: string): T {
const accessToken = localStorage.getItem(JwtHelperService.USER_ACCESS_TOKEN);
return this.getValueFromToken(accessToken, key);
}
/**
* Gets a named value from the user access token.
* @param key accessToken
* @param key Key name of the field to retrieve
* @returns Value from the token
*/
getValueFromToken<T>(accessToken: string, key: string): T {
let value;
if (accessToken) {
const tokenPayload = this.decodeToken(accessToken);
value = tokenPayload[key];
}
return <T> value;
}
}