In my service, I currently have a variable named token that determines whether or not I am authenticated. Whenever the application needs to verify if the user is signed in, it checks with !!this.security.token
and responds accordingly. This process must take place in every secure component.
ngOnInit() {
if (!this.security.token)
this.router.navigate(["/login"]);
...
}
I am curious if there is a more efficient way to handle this situation. I am contemplating the comparison of the HTTP interceptor that enriches headers and monitors my GETs and POSTs, which proves to be quite practical. However, searching for information on intercepting angular routers only yields results related to HTTP injection rather than routing control.
Could there possibly be a method to intercept each call to a component and validate the existence of the token?
export class SomeComponent implements OnInit {
constructor(
private security: SecurityService,
private router: Router,
private route: ActivatedRoute) { }
id: string;
ngOnInit() {
if (!this.security.token)
this.router.navigate(["/"]);
this.route.params
.subscribe(suc => this.id = suc.id);
}
}