Fixed issue when reload protected page with expired token

This commit is contained in:
Giuseppe Digilio
2018-04-10 19:14:42 +02:00
parent 04be7170c3
commit 34e3e4bd2b

View File

@@ -134,8 +134,17 @@ export class AuthService {
* Checks if token is present into storage and is not expired * Checks if token is present into storage and is not expired
*/ */
public checkAuthenticationToken(): Observable<AuthTokenInfo> { public checkAuthenticationToken(): Observable<AuthTokenInfo> {
const token = this.getToken(); return this.store.select(getAuthenticationToken)
return isNotEmpty(token) && !this.isTokenExpired() ? Observable.of(token) : Observable.throw(false); .map((authTokenInfo: AuthTokenInfo) => {
let token: AuthTokenInfo;
// Retrieve authentication token info and check if is valid
token = isNotEmpty(authTokenInfo) ? authTokenInfo : this.storage.get(TOKENITEM);
if (isNotEmpty(token) && token.hasOwnProperty('accessToken') && isNotEmpty(token.accessToken) && !this.isTokenExpired(token)) {
return token;
} else {
throw false;
}
});
} }
/** /**
@@ -216,10 +225,7 @@ export class AuthService {
this.store.select(getAuthenticationToken) this.store.select(getAuthenticationToken)
.subscribe((authTokenInfo: AuthTokenInfo) => { .subscribe((authTokenInfo: AuthTokenInfo) => {
// Retrieve authentication token info and check if is valid // Retrieve authentication token info and check if is valid
token = isNotEmpty(authTokenInfo) ? authTokenInfo : this.storage.get(TOKENITEM); token = authTokenInfo || null;
if (isEmpty(token) || !token.hasOwnProperty('accessToken') || isEmpty(token.accessToken)) {
token = null;
}
}); });
return token; return token;
} }
@@ -245,8 +251,8 @@ export class AuthService {
* Check if a token is expired * Check if a token is expired
* @returns {boolean} * @returns {boolean}
*/ */
public isTokenExpired(): boolean { public isTokenExpired(token?: AuthTokenInfo): boolean {
const token = this.getToken(); token = token || this.getToken();
return token && token.expires < Date.now(); return token && token.expires < Date.now();
} }
@@ -312,7 +318,6 @@ export class AuthService {
.subscribe((redirectUrl) => { .subscribe((redirectUrl) => {
if (isNotEmpty(redirectUrl)) { if (isNotEmpty(redirectUrl)) {
if (this.platform.isBrowser) { if (this.platform.isBrowser) {
console.log('CLEAR REDIRECT!!!!')
this.clearRedirectUrl(); this.clearRedirectUrl();
} }