# plug-login

> Logs in to plug.dj.

Latest version **2.1.0** (published 2018-06-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install plug-login
pnpm add plug-login
yarn add plug-login
bun add plug-login
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-06-12 |
| First published | 2015-05-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >= 4 |
| Dependencies | 1 |
| Unpacked size | 64.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Renée Kooi |
| Maintainers | burkes, goto-bus-stop |
| Keywords | login, plug, plug.dj |

## Links

- npm: https://www.npmjs.com/package/plug-login
- Repository: https://github.com/miniplug/plug-login
- Issues: https://github.com/miniplug/plug-login/issues
- npm.io page: https://npm.io/package/plug-login

## Dependencies (1)

- [node-fetch](https://npm.io/package/node-fetch.md) ^2.1.2

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-06-12
- 2.0.0 — 2018-04-27
- 1.2.0 — 2017-09-19
- 1.1.1 — 2017-06-22
- 1.1.0 — 2017-06-03
- 1.0.2 — 2017-03-20
- 1.0.1 — 2017-02-12
- 1.0.0 — 2016-12-01
- 0.4.0 — 2016-03-20
- 0.3.1 — 2015-08-09
- 0.3.0 — 2015-06-30
- 0.2.0 — 2015-06-21
- 0.1.1 — 2015-05-31
- 0.1.0 — 2015-05-31
- 0.0.1 — 2015-05-29

## README

plug-login
==========

Logs into plug.dj using a email address and password.

[![Travis](https://img.shields.io/travis/miniplug/plug-login.svg?style=flat-square)](https://travis-ci.org/miniplug/plug-login) [![NPM](https://img.shields.io/npm/v/plug-login.svg?style=flat-square)](https://npmjs.com/package/plug-login)

## Usage

```javascript
const plugLogin = require('plug-login')

plugLogin('my-plug-email@example.com', 'hunter2')
  .then(rejoice)
  .catch(() => {
    // Login failed
  })
```

## API

<a id="pluglogin-user"></a>
### plugLogin(email, password, opts={})
### plugLogin.user(email, password, opts={})

Logs in to plug.dj using the given email address and password. You can
optionally pass options in the third parameter.

Pass `{ authToken: true }` in the options `opts` to also generate a WebSocket
authentication token. (See below.) Other properties are passed through to
[`node-fetch`](https://github.com/bitinn/node-fetch).

Returns a promise. The promise resolves with an object with the properties,
`{ body, session, cookie, token }`, where `body` is plug.dj's login response,
`session` is the session token, `cookie` is a cookie string with the session
token filled in, and `token` is the auth token (if you asked for one). You can
then use the cookie string for `Cookie:` headers in subsequent requests so
plug.dj will recognise you, and you can use the auth token to set up a
connection to the plug.dj WebSocket server.

Using the cookie string with the `node-fetch` library:

```javascript
const fetch = require('node-fetch')
async function main () {
  const result = await plugLogin('fetch@example.com', 'fetch-is-small-and-good')
  const response = await fetch('https://plug.dj/_/users/me', {
    headers: { cookie: result.cookie }
  })
  const body = await response.json()
  console.log('logged in as', body.data[0])
})
```

Using the cookie string with the `request` library:

```javascript
const request = require('request')
let jar = request.jar()
plugLogin('admin@plug.dj', 'hunter3').then((result) => {
  // Store it in a jar for the correct domain.
  jar.setCookie(result.cookie, 'https://plug.dj/')
  request('https://plug.dj/_/users/me', { jar: jar, json: true }, (err, response) => {
    console.log('logged in as', response.body.data[0])
  })
})
```

<a id="pluglogin-guest"></a>
### plugLogin(opts={})
### plugLogin.guest(opts={})

Gets a plug.dj session cookie and, optionally, WebSocket authentication token
as a guest user.

`opts` takes the same options as user-style `plugLogin()`.

Returns a promise that resolves with an object with the properties,
`{ session, cookie, token }`. See [plugLogin.user](#pluglogin-user) for what
those properties mean.

```javascript
// "logging in" as a guest
plugLogin.guest({ authToken: true }).then((result) => {
  // result.token contains an authentication token for the plug.dj WebSocket.
  require('plug-socket')(result.token)
})
```

## License

[MIT](./LICENSE)

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