Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

auth-bearer-parser

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

auth-bearer-parser

This is a parsing middleware for Bearer tokens that can be used with the Express framework. Parse the `Authorization` header and assign the Bearer token to `req.token`.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-80%
Maintainers
1
Weekly downloads
 
Created
Source

auth-bearer-parser

This is a parsing middleware for Bearer tokens that can be used with the Express framework.
Parse the Authorization header and assign the Bearer token to req.token.

Installation

npm

$ npm install bearer-token-parser

yarn

$ yarn add bearer-token-parser

Usage

TypeScript

import express, { Request, Response } from 'express';
import authBearerParser from 'auth-bearer-parser';

const app = express();
app.use(authBearerParser());
//=> now you have access to req.token, which contains the Bearer token

app.get('/', (req: Request, res: Response) => {
	console.log(req.token);
	...
});
...

JavaScript

ES Module
import express from 'express';
import authBearerParser from 'auth-bearer-parser';

const app = express();
app.use(authBearerParser());
//=> now you have access to req.token, which contains the Bearer token

app.get('/', (req, res) => {
	console.log(req.token);
	...
});
...
CommonJS

Note that you should be require('...').default.

const express = require('express');
const authBearerParser = require('auth-bearer-parser').default;

const app = express();
app.use(authBearerParser());
//=> now you have access to req.token, which contains the Bearer token

app.get('/', (req, res) => {
	console.log(req.token);
	...
});
...

API

authBearerParser(options) -> void

options

Optional
Type: object

isThrowError

Optional
Type: boolean
Default: false

If true, throw error when bearer token is invalid.
The error objects thrown are as follows.

statusmessage
401authorization header missing
400invalid token type: ${auth-scheme}
auth-scheme is Basic or Digest and so on
401token missing

To catch errors thrown and continue processing, the default error handling of express can be changed to any error by extending it. An example is shown below.

import express from 'express';
import authBearerParser from 'auth-bearer-parser';

const app = express();
app.use(authBearerParser({ isThrowError: true })); // throw error when bearer token is invalid

// some kind of processing (API implementation by router, etc.)

// override express default error handlers (https://expressjs.com/en/guide/error-handling.html#writing-error-handlers)
app.use((err, req, res, next) => {
	// you can access property `status` and `message`
	res.status(err.status | 500).json({ message: err.message });
});

License

MIT licensed

Keywords

FAQs

Package last updated on 03 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc