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

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-11-27 18:33:17 +01:00
import { AfterViewInit, Component, Input } from '@angular/core';
2018-10-05 17:00:54 +02:00
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from '@app/core/services/auth/auth.service';
import { BaseComponent } from '@common/base/base.component';
2018-11-27 18:33:17 +01:00
import { takeUntil } from 'rxjs/operators';
2017-12-18 16:55:12 +01:00
@Component({
2018-10-05 17:00:54 +02:00
selector: 'app-unauthorized-component',
templateUrl: './unauthorized.component.html'
2017-12-18 16:55:12 +01:00
})
2018-11-27 18:33:17 +01:00
export class UnauthorizedComponent extends BaseComponent implements AfterViewInit {
2018-10-05 17:00:54 +02:00
@Input()
public message: string;
constructor(
private authService: AuthService,
private route: ActivatedRoute,
private router: Router
2018-11-27 18:33:17 +01:00
) { super(); }
2017-12-18 16:55:12 +01:00
2018-10-05 17:00:54 +02:00
ngAfterViewInit() {
const returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
const principal = this.authService.current();
if (!principal) {
this.router.navigate(['/login'], { queryParams: { returnUrl: returnUrl } });
} else {
2018-11-27 18:33:17 +01:00
this.authService.me()
.pipe(takeUntil(this._destroyed))
.subscribe(
result => {
if (!result) { this.router.navigate(['/login'], { queryParams: { returnUrl: returnUrl } }); } else { this.router.navigate(['/']); }
},
err => console.error('An error occurred', err));
2018-10-05 17:00:54 +02:00
}
}
}