Making an Authenticated Route
Understand how to protect web routes in a Deno application by implementing JWT-based authentication middleware. Learn to integrate oak-middleware-jwt to validate tokens sent in authorization headers, ensuring only authenticated users access specific routes. This lesson guides you through configuring your server to handle authorization securely while testing the protected endpoints effectively.
We'll cover the following...
Add JWT middleware route
Having the capacity to get users a token, we now want a guarantee that only logged-in users can access the museum route.
Users will have to send the token in the Authorization header because the JWT token standard defines. If the token is invalid or not present, the user should be presented with a 401 Unauthorized status code.
Validating the token that’s been sent by users on the request is a nice use case for middleware functions.
In order to do this, and since we’re using Oak, we’ll be using a third-party module named oak-middleware-jwt. This is nothing more than a middleware that automatically validates the JWT, based on a key, and provides functionality that will be useful
to us.
You can check its documentation at https://nest.land/package/oak-middleware-jwt.
Let’s use this middleware in our web code to make the museums route ...