Skip to content
Advertisement

How to create ActionFilter in Javascript?

I understand that there is a concept of Action Filters in languages like C# using ASP.NET. I would like to have this kind of pre-processing functionality in my class methods, using Javascript. Is this possible/a part of the language? Otherwise how would I implement this feature? It sounds like a language level implementation, like constructors and getters, and so I’m confused whether or not it’s possible to implement such a thing.

TMI: I’m trying to create an ecommerce website with firebase auth. Right now I have implemented all the cart methods; but I would like to have some sort of auth check to allow these methods to be called, or otherwise route the user to the login page.

Advertisement

Answer

While I couldn’t find the exact functionality of an Action Filter, JS has a neat feature called Proxy‘s, which can be used for the same purpose. When you create a proxy of an object, it intercept the requests to the given object, and sort of acts like an intermediary. What I found is that you can include the processing logic of an Action Filter, such as checking if a user is authorized, inside the get and set of the handler provided to the proxy.

JavaScript

This eliminates the need to modify each method of the base class, just to include that one check(or multiple checks; that can also be done). Doing so, we separate the functionalities. Check out this neat video for more info.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement