# token-extractor

> Bearer token extractor for Node.js

Latest version **0.1.6** (published 2016-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install token-extractor
pnpm add token-extractor
yarn add token-extractor
bun add token-extractor
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2016-05-24 |
| First published | 2016-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Joshua Marquez |
| Maintainers | joshua.marquez |
| Keywords | bearer, token, access_token, authorization |

## Links

- npm: https://www.npmjs.com/package/token-extractor
- Repository: https://github.com/joshuamarquez/node-token-extractor
- Homepage: https://github.com/joshuamarquez/node-token-extractor#readme
- Issues: https://github.com/joshuamarquez/node-token-extractor/issues
- npm.io page: https://npm.io/package/token-extractor

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 0.1.6 (latest) — 2016-05-24
- 0.1.5 — 2016-05-24
- 0.1.4 — 2016-05-24
- 0.1.2 — 2016-05-17
- 0.1.0 — 2016-05-17

## README

# node-token-extractor

[![npm version](https://badge.fury.io/js/token-extractor.svg)](https://badge.fury.io/js/token-extractor)

Extracts `access_token` from a HTTP request header `Authorization: Bearer <access_token>`

## Install

```
$ npm install token-extractor --save
```

## Usage

### tokenExtractor(req, callback)

**Arguments**

* `req`: HTTP Node.js request
* `callback(err, token)`: A callback which is called with either the token extracted from the header or a possible error.

**Example**

```javascript
const tokenExtractor = require('token-extractor');

tokenExtractor(req, (err, token) => {
  if (err) {
    // handle error
  }

  console.log(token); // token extracted from HTTP header
});
```

## Error handling

### TokenExtractorError

**Possible thrown errors**

| message                                         | code                                 |
| ----------------------------------------------- |:------------------------------------:|
| No Authorization header is present              | `E_AUTHORIZATION_REQUIRED`           |
| Format is :: Authorization: Bearer <token>      | `E_AUTHORIZATION_INVALID_FORMAT`     |
| Authorization token was not found               | `E_AUTHORIZATION_TOKEN_NOT_FOUND`    |

**Example**

Suppose `E_AUTHORIZATION_TOKEN_NOT_FOUND` error was thrown

```javascript
tokenExtractor(req, (err, token) => {
  if (err) {
    console.log(err.toJSON());
    /*
      {
        status: 401,
        message: 'Authorization token was not found',
        code: 'E_AUTHORIZATION_TOKEN_NOT_FOUND'
      }
    */

    console.log(err.toString());
    /*
      [TokenExtractedError (E_AUTHORIZATION_TOKEN_NOT_FOUND) Authorization token was not found]
    */

    console.trace(err);
    /*
      prints Error Stack since err instanceof Error
    */
  }
}));
```

## Test

```
$ npm test
```

---
_Source: https://npm.io/package/token-extractor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
