Added redirect to login whether token is expired

This commit is contained in:
Giuseppe Digilio
2018-04-04 13:32:02 +02:00
parent 5fc6d4ead8
commit f841612e1c
4 changed files with 34 additions and 10 deletions

View File

@@ -17,6 +17,7 @@ import { AuthTokenInfo } from './models/auth-token-info.model';
import { isNotEmpty, isUndefined } from '../../shared/empty.util';
import { RedirectWhenTokenExpiredAction, RefreshTokenAction } from './auth.actions';
import { Store } from '@ngrx/store';
import { Router } from '@angular/router';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
@@ -26,7 +27,7 @@ export class AuthInterceptor implements HttpInterceptor {
// we're creating a refresh token request list
protected refreshTokenRequestUrls = [];
constructor(private inj: Injector, private store: Store<AppState>) { }
constructor(private inj: Injector, private router: Router, private store: Store<AppState>) { }
private isUnauthorized(response: HttpResponseBase): boolean {
// invalid_token The access token provided is expired, revoked, malformed, or invalid for other reasons
@@ -38,7 +39,7 @@ export class AuthInterceptor implements HttpInterceptor {
}
private isAuthRequest(http: HttpRequest<any> | HttpResponseBase): boolean {
return http.url
return http && http.url
&& (http.url.endsWith('/authn/login')
|| http.url.endsWith('/authn/logout')
|| http.url.endsWith('/authn/status'));
@@ -71,10 +72,16 @@ export class AuthInterceptor implements HttpInterceptor {
const authService = this.inj.get(AuthService);
const token = authService.getToken();
let newReq;
// Intercept a request that is not to the authentication endpoint
if (!this.isAuthRequest(req) && isNotEmpty(token)) {
if (authService.isTokenExpired()) {
authService.setRedirectUrl(this.router.url);
// The access token is expired
// Redirect to the login route
this.store.dispatch(new RedirectWhenTokenExpiredAction('Your session has expired. Please log in again.'));
return Observable.of(null);
} else if (!this.isAuthRequest(req) && isNotEmpty(token)) {
// Intercept a request that is not to the authentication endpoint
authService.isTokenExpiring()
.filter((isExpiring) => isExpiring)
.subscribe(() => {