micro-basic-auth

1.1.1 • Public • Published

micro-basic-auth Build Status npm version Dependency Status

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

/micro-basic-auth/

    Package Sidebar

    Install

    npm i micro-basic-auth

    Weekly Downloads

    4

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    8.65 kB

    Total Files

    7

    Last publish

    Collaborators

    • bealearts