What is the best approach to handle authentication on a bunch of Javascript actions on a page without littering the code base with “if authenticated()” checks?
For example: we have 10 like buttons, some comment buttons and a few other actions that require authentication. When a user is not authenticated, we want to redirect them to a login/signup page. However, we want to avoid littering the code with if (user.isAuthenticated()) { xxx } calls. In our particular case we want to use these mostly for events in backbone, although I don’t think that matters for the general question.
With the help of underscorejs. You can write something like this:
Suppose you’re using jQuery, when binding the events, write this:
or