middleware-jwt
HTTP requests authentication middleware for Marble.js based on JWT mechanism.
This module lets you authenticate HTTP requests using JWT tokens in your Marble.js applications. JWTs are typically used to protect API endpoints, and are often issued using OpenID Connect.
You can find more details about JWT (RFC 7519) standard here.
The middleware uses jsonwebtoken
package under the hood. It wraps the package API into more
RxJS-friendly abstractions that can be partially applied and composed inside Effect streams.
Installation
Requires @marblejs/core
to be installed.
Importing
Type declaration
Parameters
A function used for payload verification. With this handler we can check if the payload extracted from the JWT token fulfills our security criterias (eg. we can verify if the given user identifier exists in the database). Besides the general verification, the function can return the streamed object, that will be available inside HttpRequest req.user
parameter. If the stream throws an error (eg. during the validation) the authorize$ middleware responds with 401 / Unauthorized
error.
Type declaration
Example
The function checks the presence of the user id in the database and then returns an Observable of found user instance.
For more infos about jwt.VerifyOptions please read jsonwebtoken docs.
You can read more about token creation here.
Usage
It is recommended to extract the middleware configuration into separate file. This way you can reuse it in many places.
The configured middleware can be simply composed in any route, that should be validated.
If the incoming request doesn't pass the authentication process (eg. token is invalid, expired or the verifyPayload$
throws an error, the middleware responds with 401 / Unauthorized
error.
Last updated