After wrapping multiple components' ngInit with a check to verify if users are logged in or not, I am looking for a way to avoid repeating the same code.
export class ComponentX implements OnInit {
constructor(private _authService: AuthService) {}
ngOnInit() {
if(this._authService.isLoggedIn()) {
// Do stuff
}
else {
// Redirect to login page
}
}
}
I want to consolidate this if check into a single location that can be easily applied to any components as needed. How can I achieve this?