This commit is contained in:
Giuseppe Digilio
2018-04-10 14:27:32 +02:00
parent 0ed1f7778e
commit 1f953b6b9d
2 changed files with 31 additions and 17 deletions

View File

@@ -19,6 +19,7 @@ import { ResetAuthenticationMessagesAction, SetRedirectUrlAction } from './auth.
import { RouterReducerState } from '@ngrx/router-store'; import { RouterReducerState } from '@ngrx/router-store';
import { CookieAttributes } from 'js-cookie'; import { CookieAttributes } from 'js-cookie';
import { NativeWindowRef, NativeWindowService } from '../../shared/services/window.service'; import { NativeWindowRef, NativeWindowService } from '../../shared/services/window.service';
import { PlatformService } from '../../shared/services/platform.service';
export const LOGIN_ROUTE = '/login'; export const LOGIN_ROUTE = '/login';
@@ -38,6 +39,7 @@ export class AuthService {
constructor(@Inject(NativeWindowService) private _window: NativeWindowRef, constructor(@Inject(NativeWindowService) private _window: NativeWindowRef,
private authRequestService: AuthRequestService, private authRequestService: AuthRequestService,
private platform: PlatformService,
private router: Router, private router: Router,
private storage: CookieService, private storage: CookieService,
private store: Store<AppState>) { private store: Store<AppState>) {
@@ -289,8 +291,13 @@ export class AuthService {
* Redirect to the login route when token has expired * Redirect to the login route when token has expired
*/ */
public redirectToLoginWhenTokenExpired() { public redirectToLoginWhenTokenExpired() {
// Hard redirect to login page, so that all state is definitely lost const redirectUrl = LOGIN_ROUTE + '?expired=true';
this._window.nativeWindow.location.href = LOGIN_ROUTE + '?expired=true'; if (this._window.nativeWindow.location) {
// Hard redirect to login page, so that all state is definitely lost
this._window.nativeWindow.location.href = redirectUrl;
} else {
this.router.navigateByUrl(redirectUrl);
}
} }
/** /**
@@ -301,24 +308,20 @@ export class AuthService {
.first() .first()
.subscribe((redirectUrl) => { .subscribe((redirectUrl) => {
if (isNotEmpty(redirectUrl)) { if (isNotEmpty(redirectUrl)) {
this.clearRedirectUrl(); if (this.platform.isBrowser) {
console.log('CLEAR REDIRECT!!!!')
this.clearRedirectUrl();
}
const urlTree: UrlTree = this.router.parseUrl(redirectUrl);
const g: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET];
const segment = '/' + g.toString();
const navigationExtras: NavigationExtras = {
queryParams: urlTree.queryParams,
queryParamsHandling: 'merge'
};
this.router.navigate([segment], navigationExtras);
} else {
// override the route reuse strategy // override the route reuse strategy
this.router.routeReuseStrategy.shouldReuseRoute = () => { this.router.routeReuseStrategy.shouldReuseRoute = () => {
return false; return false;
}; };
this.router.navigated = false; this.router.navigated = false;
const url = decodeURIComponent(this.router.url); const url = decodeURIComponent(redirectUrl);
this.router.navigateByUrl(url); this.router.navigateByUrl(url);
} else {
this.router.navigate(['/']);
} }
}) })
@@ -328,6 +331,7 @@ export class AuthService {
* Refresh route navigated * Refresh route navigated
*/ */
public refreshAfterLogout() { public refreshAfterLogout() {
this.router.navigate(['/home']);
// Hard redirect to home page, so that all state is definitely lost // Hard redirect to home page, so that all state is definitely lost
this._window.nativeWindow.location.href = '/home'; this._window.nativeWindow.location.href = '/home';
} }
@@ -348,7 +352,13 @@ export class AuthService {
* Set redirect url * Set redirect url
*/ */
setRedirectUrl(url: string) { setRedirectUrl(url: string) {
this.storage.set(REDIRECT_COOKIE, url); // Add 1 day to the current date
const expireDate = Date.now() + (1000 * 60 * 60 * 24 * 1);
// Set the cookie expire date
const expires = new Date(expireDate);
const options: CookieAttributes = {expires: expires};
this.storage.set(REDIRECT_COOKIE, url, options);
this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : '')); this.store.dispatch(new SetRedirectUrlAction(isNotUndefined(url) ? url : ''));
} }

View File

@@ -8,8 +8,12 @@ export class AuthTokenInfo {
constructor(token: string) { constructor(token: string) {
this.accessToken = token.replace('Bearer ', ''); this.accessToken = token.replace('Bearer ', '');
const tokenClaims = decode(this.accessToken); try {
// exp claim is in seconds, convert it se to milliseconds const tokenClaims = decode(this.accessToken);
this.expires = tokenClaims.exp * 1000; // exp claim is in seconds, convert it se to milliseconds
this.expires = tokenClaims.exp * 1000;
} catch (err) {
this.expires = 0;
}
} }
} }