micro-basic-auth
v1.1.1
Published
Basic Auth for micro based micro-services
Downloads
6
Maintainers
Readme
micro-basic-auth
Basic Auth for micro based micro-services
API compatible with the microauth modules
Usage
import { send } from 'micro';
import basicAuth, { challenge } from 'micro-basic-auth';
/** Use default validate **/
const options = {
realm: 'MyApp',
username: 'Bob',
password: 'secret'
};
/** or supply a function **/
const options = {
realm: 'MyApp',
validate: async (username, password, options) => {
return true // if valid
}
};
/********/
// Third `auth` argument will provide error or result of authentication
// so it will { err: errorObject} or { result: {
// provider: 'basic',
// info: userInfo
// }}
const handler = async (req, res, auth) => {
if (!auth) {
return send(res, 404, 'Not Found');
}
if (auth.err) {
// Error handler
console.error(auth.err);
// If you want to prompt for credentials again
challenge(res, auth);
return send(res, 401, 'Access denied');
// Otherwise
return send(res, 403, 'Forbidden');
}
return `Hello ${auth.result.info.username}`;
};
export default basicAuth(options)(handler);
Install
npm i micro-basic-auth