import { AuthService } from '../services/auth/auth.service'; import { Component, Input } from "@angular/core"; import { ActivatedRoute, Router } from "@angular/router"; @Component({ selector: 'unauthorized-component', templateUrl: './unauthorized.component.html' }) export class UnauthorizedComponent { @Input() public message: string; constructor( private authService: AuthService, private route: ActivatedRoute, private router: Router ) { } ngAfterViewInit() { let returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; let principal = this.authService.current(); if (!principal) { this.router.navigate(['/login'], { queryParams: { returnUrl: returnUrl } }); } else { this.authService.me().subscribe( principal => { if (!principal) this.router.navigate(['/login'], { queryParams: { returnUrl: returnUrl } }); else this.router.navigate(["/"]) }, err => console.error('An error occurred', err)); } } }