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] = 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
middlewarearray 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
realmoption let's you set the realm for the authentication, this is the name of the protected area.The
useroption let's you set the username and password to authenticate, you can also pass an array of users.The
verifyUseroption let's you pass a function to verify the user, this can be useful to check the user against a database.The
verifyUserfunction should returntrueif the user is authenticated, andfalseotherwise.In case of an invalid username or password the middleware will return a
401status code with aWWW-Authenticateheader.The
invalidUserMessageoption 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
invalidUserMessageby 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Ã