I want to create an authentication decorator in my application.
Usage example 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;
}
}
The problem for me is how to properly initialize userService and navigationService in this case, since these services contain all logic for finding if a user is authenticated and showing login screen.
I already tried:
Any hints would be helpful. Could be that I miss something fundamental since I'm not an experienced angular developer or there is another way to approach this problem.
As explained in this answer, the solution that is idiomatic to the framework is to expose injector
class instance property, so it could be accessed inside decorator. The existence of injector
property can be also secured with an interface.
Since property decorator runs once and has access to class prototype but not instance, it's necessary to patch ngOnOnit
method and retrieve all necessary services inside patched method with this.injector.get()
.
The alternative is to expose global injector to some object, as explained here. This is not idiomatic solution but a hack that will result in certain limitations and negative consequences. It can hardly be recommended for use in production.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With