import { Component, OnInit } from '@angular/core';
-import { Router } from '@angular/router';
+import { ActivatedRoute, Router } from '@angular/router';
+
+import * as _ from 'lodash';
import { BsModalService } from 'ngx-bootstrap/modal';
export class LoginComponent implements OnInit {
model = new Credentials();
isLoginActive = false;
+ returnUrl: string;
constructor(
private authService: AuthService,
private authStorageService: AuthStorageService,
private bsModalService: BsModalService,
+ private route: ActivatedRoute,
private router: Router
) {}
login() {
this.authService.login(this.model).then(() => {
- this.router.navigate(['']);
+ const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/');
+ this.router.navigate([url]);
});
}
}
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: AuthGuardService;
let authStorageService: AuthStorageService;
let ngZone: NgZone;
+ let route: ActivatedRouteSnapshot;
+ let state: RouterStateSnapshot;
@Component({ selector: 'cd-login', template: '' })
class LoginComponent {}
});
it('should allow the user if loggedIn', () => {
+ route = null;
+ state = { url: '/', root: null };
spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
- expect(service.canActivate()).toBe(true);
+ expect(service.canActivate(route, state)).toBe(true);
});
it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => {
const router = TestBed.get(Router);
+ state = { url: '/pool', root: null };
ngZone.run(() => {
- expect(service.canActivate()).toBe(false);
+ expect(service.canActivate(route, state)).toBe(false);
});
tick();
- expect(router.url).toBe('/login');
+ expect(router.url).toBe('/login?returnUrl=%2Fpool');
}));
});
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 AuthGuardService implements CanActivate, CanActivateChild {
constructor(private router: Router, private authStorageService: AuthStorageService) {}
- canActivate() {
+ canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authStorageService.isLoggedIn()) {
return true;
}
- this.router.navigate(['/login']);
+ this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
- canActivateChild(): boolean {
- return this.canActivate();
+ canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
+ return this.canActivate(childRoute, state);
}
}