cloudflare-workers-auth

0.0.2 • Public • Published

cloudflare-workers-auth

API key authentication for Cloudflare Workers

Install

$ npm install cloudflare-workers-auth cloudflare-workers

Usage

const worker = require('cloudflare-workers');
const auth = require('cloudflare-workers-auth');
 
auth.init({
    apiKeyHeader: 'x-apikey',
    apiKeys: [ 'key123', 'keyabc', 'keyxyz' ]
})
 
worker.get('/', (request) => new Response('welcome\n'));
worker.use('/protected*', auth.apiKey);
worker.get('/protected', (request) => new Response('path protected by apikey\n'));
worker.get('/protected/account', (request) => new Response('account info: apikey protected\n'));
worker.get('/protected/address', (request) => new Response('address details: apikey protected\n'));
worker.get('/public', (request) => new Response('public path: not apikey protected\n'));
 
addEventListener('fetch', function(event) {
    event.respondWith(worker.handleRequest(event));
});

See 'Using NPM modules' to require the package in your worker.

Design

This package is designed as middleware for the 'cloudflare-workers' routing package.

License

MIT license; see LICENSE.

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i cloudflare-workers-auth

      Weekly Downloads

      0

      Version

      0.0.2

      License

      MIT

      Unpacked Size

      3.54 kB

      Total Files

      4

      Last publish

      Collaborators

      • bitquant