
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
auth-bearer-parser
Advanced tools
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`.
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
.
$ npm install auth-bearer-parser
$ yarn add auth-bearer-parser
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);
...
});
...
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);
...
});
...
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);
...
});
...
authBearerParser(options)
-> void
Optional
Type: object
Optional
Type: boolean
Default: false
If true, throw error when bearer token is invalid.
The error objects thrown are as follows.
status | message |
---|---|
401 | authorization header missing |
400 | invalid token type: ${auth-scheme} ※ auth-scheme is Basic or Digest and so on |
401 | token 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 });
});
FAQs
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`.
The npm package auth-bearer-parser receives a total of 2 weekly downloads. As such, auth-bearer-parser popularity was classified as not popular.
We found that auth-bearer-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.