argos/dmp-frontend/src/app/unauthorized/unauthorized.component.ts

32 lines
1015 B
TypeScript

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