Search code examples
node.jsmiddlewarehttp-proxy-middleware

How can I create a global middleware to validate subscriptions?


For the architecture that is intended to be used for the project, it is necessary to have a middleware that validates if a user has an active subscription, that is, if he has already paid for it.


Solution

  • There's not enough in your question for us to have any idea how to help you in any specific way. In a nutshell, you require the user to login (presumably looking them up in a user database), then you create middleware that checks if the user is logged in and if that login has an active subscription (presumably by looking something up in your subscription database). If they do, then you call next() to continue routing to your other routes. If not, then you redirect them to some page describing that they need a subscription.

    You would place this middleware as one of the first routes, only after the routes to login and to create an account (and after any other routes that don't require login/subscription authentication). Just make sure it is before any routes that require the subscription auth and then they will all be protected.