• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# registry-auth-token
2
3[![npm version](http://img.shields.io/npm/v/registry-auth-token.svg?style=flat-square)](http://browsenpm.org/package/registry-auth-token)[![Build Status](http://img.shields.io/travis/rexxars/registry-auth-token/master.svg?style=flat-square)](https://travis-ci.org/rexxars/registry-auth-token)
4
5Get the auth token set for an npm registry from `.npmrc`. Also allows fetching the configured registry URL for a given npm scope.
6
7## Installing
8
9```
10npm install --save registry-auth-token
11```
12
13## Usage
14
15Returns an object containing `token` and `type`, or `undefined` if no token can be found. `type` can be either `Bearer` or `Basic`.
16
17```js
18var getAuthToken = require('registry-auth-token')
19var getRegistryUrl = require('registry-auth-token/registry-url')
20
21// Get auth token and type for default `registry` set in `.npmrc`
22console.log(getAuthToken()) // {token: 'someToken', type: 'Bearer'}
23
24// Get auth token for a specific registry URL
25console.log(getAuthToken('//registry.foo.bar'))
26
27// Find the registry auth token for a given URL (with deep path):
28// If registry is at `//some.host/registry`
29// URL passed is `//some.host/registry/deep/path`
30// Will find token the closest matching path; `//some.host/registry`
31console.log(getAuthToken('//some.host/registry/deep/path', {recursive: true}))
32
33// Find the configured registry url for scope `@foobar`.
34// Falls back to the global registry if not defined.
35console.log(getRegistryUrl('@foobar'))
36
37// Use the npm config that is passed in
38console.log(getRegistryUrl('http://registry.foobar.eu/', {
39  npmrc: {
40    'registry': 'http://registry.foobar.eu/',
41    '//registry.foobar.eu/:_authToken': 'qar'
42  }
43}))
44```
45
46## Return value
47
48```js
49// If auth info can be found:
50{token: 'someToken', type: 'Bearer'}
51
52// Or:
53{token: 'someOtherToken', type: 'Basic'}
54
55// Or, if nothing is found:
56undefined
57```
58
59## Security
60
61Please be careful when using this. Leaking your auth token is dangerous.
62
63## License
64
65MIT-licensed. See LICENSE.
66