1.1.1 • Published 6 years ago

micro-basic-auth v1.1.1

Weekly downloads
54
License
MIT
Repository
github
Last release
6 years ago

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
1.1.1

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago