The JWK Auth middleware let's you add a JSON Web Key authentication to your routes, this can be useful to protect routes that need to be private and will be accessed by other services.
Warning: JWK Auth is more secure than Basic Auth, but it should be used with HTTPS to ensure the token is encrypted.
exportconst [jwkAuthMiddleware] = unstable_createJWKAuthMiddleware({ invalidTokenMessage({ request, context }) { // do something with request or context here return { message:`Invalid token` }; }, jwksUri:"https://auth.example.com/.well-known/jwks.json", });
In both cases, with a hard-coded value or a function, the invalid message can be a string or an object, if it's an object it will be converted to JSON.
If you save a JWT in a cookie using React Router's Cookie API, you can tell the middleware to look for the token in the cookie instead of the Authorization header.
The JWK Auth middleware let's you add a JSON Web Key authentication to your routes, this can be useful to protect routes that need to be private and will be accessed by other services.
The
jwksUri
option let's you set the URL to the JWKS endpoint, this is the URL where the public keys are stored.To use the middleware, you need to add it to the
unstable_middleware
array in the route where you want to use it.Now, when you access the route it will check the JWT token in the
Authorization
header.In case of an invalid token the middleware will return a
401
status code with aWWW-Authenticate
header.The
realm
option let's you set the realm for the authentication, this is the name of the protected area.If you want to customize the message sent when the token is invalid you can use the
invalidTokenMessage
option.And this will be the response when the token is invalid.
You can also customize the
invalidTokenMessage
by passing a function which will receive the Request and context objects.In both cases, with a hard-coded value or a function, the invalid message can be a string or an object, if it's an object it will be converted to JSON.
If you want to get the JWT payload in your loaders, actions, or other middleware you can use the
getJWTPayload
function.And you can use the payload to get the subject, scope, issuer, audience, or any other information stored in the token.
With a Custom Header
If your app receives the JWT in a custom header instead of the
Authorization
header you can tell the middleware to look for the token in that header.Now use the middleware as usual, but now instead of looking for the token in the
Authorization
header it will look for it in theX-API-Key
header.With a Cookie
If you save a JWT in a cookie using React Router's Cookie API, you can tell the middleware to look for the token in the cookie instead of the
Authorization
header.Then use the middleware as usual, but now instead of looking for the token in the
Authorization
header it will look for it in the cookie.Author
Sergio XalambrÃ