I want to create authentication decorator in my application.
Call should be simple as
@RequireAuthentication()
@HostListener('click', ['$event']) onClick(event: Event) {
....
}
As I know decorator can only be function, so in some other file I plan to have
export function RequireAuthentication() {
if (!userService.isAuthenticated) {
navigationService.goToLogin();
return;
}
}
Problem for me is how to properly initialize userService and navigationService in this case, since these services contains all logic for finding if user is authenticated and showing login screen.
I already tried:
- to use class with constructor for service initalization, but then nested method cannot be used as decorator
- to use Injectable class to create services, I need to create instance of this class, same problem.
- to use ModuleWithProviders approach to hide authentication implementation and only expose decorator, but not sure if that is right way to do this.
Any hints would be helpful. Could be that I miss something fundamental, since I'm not experienced angular developer or there is another way to approach this problem.
Thanks in advance!