Express Bearer Token Middleware

3.0.0 · maintenance · verified Wed Apr 22

express-bearer-token is an Express middleware for extracting RFC6750-compliant OAuth 2.0 bearer tokens from incoming HTTP requests. It attempts to locate a token in the 'Authorization: Bearer <token>' header, the 'access_token' field in the request body, or 'access_token' in query parameters. Optionally, it can also extract tokens from cookies. If found, the token is made available on `req.token`. Crucially, if multiple token sources are present, the middleware strictly adheres to RFC6750 by immediately aborting the request with an HTTP 400 status code. The package is currently at version 3.0.0 and ships with TypeScript types. Its release cadence appears to be slow, with the last major release two years ago, suggesting a mature, maintenance-focused project rather than active feature development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of the `express-bearer-token` middleware to extract a bearer token from various sources and make it available on `req.token` for subsequent route handlers.

import express from 'express';
import bearerToken from 'express-bearer-token';

const app = express();

app.use(bearerToken());

app.get('/', (req, res) => {
  if (req.token) {
    res.send('Token found: ' + req.token);
  } else {
    res.status(401).send('No token provided');
  }
});

app.listen(8000, () => {
  console.log('Server listening on port 8000.\nTest with: `curl -H "Authorization: Bearer mytoken" localhost:8000`');
  console.log('Or: `curl -X POST -d "access_token=bodytoken" localhost:8000`');
});

view raw JSON →