This depends on @oslojs/crypto, and @oslojs/encoding.
The Basic Auth middleware let's you add a basic authentication to your
routes, this can be useful to protect routes that need to be private.
Warning: Basic Auth is not secure by itself, it should be used with
HTTPS to ensure the username and password are encrypted. Do not use it to
protect sensitive data, use a more secure method instead.
exportconst [basicAuthMiddleware] = unstable_createBasicAuthMiddleware({ invalidUserMessage({ request, context }) { // do something with request or context here return { message:`Invalid username or password for ${username}` }; }, user: { username:"admin", password:"password" }, });
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.
The Basic Auth middleware let's you add a basic authentication to your routes, this can be useful to protect routes that need to be private.
To use it, 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 you will be prompted to enter the username and password.
The
realm
option let's you set the realm for the authentication, this is the name of the protected area.The
user
option let's you set the username and password to authenticate, you can also pass an array of users.The
verifyUser
option let's you pass a function to verify the user, this can be useful to check the user against a database.The
verifyUser
function should returntrue
if the user is authenticated, andfalse
otherwise.In case of an invalid username or password the middleware will return a
401
status code with aWWW-Authenticate
header.The
invalidUserMessage
option let's you customize the message sent when the user is invalid.And this will be the response when the user is invalid.
You can also customize the
invalidUserMessage
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.
Author
Sergio XalambrÃ