import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
-import { Router } from '@angular/router';
+import { ActivatedRoute, Router } from '@angular/router';
+import * as _ from 'lodash';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
constructor(
private authStorageService: AuthStorageService,
private http: HttpClient,
- private router: Router
+ private router: Router,
+ private route: ActivatedRoute
) {}
check(token: string) {
logout(callback: Function = null) {
return this.http.post('api/auth/logout', null).subscribe((resp: any) => {
this.authStorageService.remove();
- this.router.navigate(['/login'], { skipLocationChange: true });
+ const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/login');
+ this.router.navigate([url], { skipLocationChange: true });
if (callback) {
callback();
}
import { Component, NgZone } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
-import { Router, Routes } from '@angular/router';
+import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { configureTestBed } from '~/testing/unit-test-helper';
let service: ChangePasswordGuardService;
let authStorageService: AuthStorageService;
let ngZone: NgZone;
+ let route: ActivatedRouteSnapshot;
+ let state: RouterStateSnapshot;
@Component({ selector: 'cd-login-password-form', template: '' })
class LoginPasswordFormComponent {}
it('should do nothing (not logged in)', () => {
spyOn(authStorageService, 'isLoggedIn').and.returnValue(false);
- expect(service.canActivate()).toBeTruthy();
+ expect(service.canActivate(route, state)).toBeTruthy();
});
it('should do nothing (SSO enabled)', () => {
spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
spyOn(authStorageService, 'isSSO').and.returnValue(true);
- expect(service.canActivate()).toBeTruthy();
+ expect(service.canActivate(route, state)).toBeTruthy();
});
it('should do nothing (no update pwd required)', () => {
spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(false);
- expect(service.canActivate()).toBeTruthy();
+ expect(service.canActivate(route, state)).toBeTruthy();
});
- it('should redirect to change password page', fakeAsync(() => {
+ it('should redirect to change password page by preserving the query params', fakeAsync(() => {
+ route = null;
+ state = { url: '/host', root: null };
spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
spyOn(authStorageService, 'isSSO').and.returnValue(false);
spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(true);
const router = TestBed.inject(Router);
ngZone.run(() => {
- expect(service.canActivate()).toBeFalsy();
+ expect(service.canActivate(route, state)).toBeFalsy();
});
tick();
- expect(router.url).toBe('/login-change-password');
+ expect(router.url).toBe('/login-change-password?returnUrl=%2Fhost');
}));
});
import { Injectable } from '@angular/core';
-import { CanActivate, CanActivateChild, Router } from '@angular/router';
+import {
+ ActivatedRouteSnapshot,
+ CanActivate,
+ CanActivateChild,
+ Router,
+ RouterStateSnapshot
+} from '@angular/router';
import { AuthStorageService } from './auth-storage.service';
export class ChangePasswordGuardService implements CanActivate, CanActivateChild {
constructor(private router: Router, private authStorageService: AuthStorageService) {}
- canActivate() {
+ canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
// Redirect to '/login-change-password' when the following constraints
// are fulfilled:
// - The user must be logged in.
!this.authStorageService.isSSO() &&
this.authStorageService.getPwdUpdateRequired()
) {
- this.router.navigate(['/login-change-password']);
+ this.router.navigate(['/login-change-password'], { queryParams: { returnUrl: state.url } });
return false;
}
return true;
}
- canActivateChild(): boolean {
- return this.canActivate();
+ canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
+ return this.canActivate(childRoute, state);
}
}