# feathers-authentication-client

> The authentication plugin for feathers-client

Latest version **0.3.3** (published 2017-07-18) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install feathers-authentication-client
pnpm add feathers-authentication-client
yarn add feathers-authentication-client
bun add feathers-authentication-client
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.3 |
| Published | 2017-07-18 |
| First published | 2016-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 4.6.0 |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 41 |
| Author | Feathers contributors |
| Maintainers | daffl, ekryski, marshallswain |
| Keywords | feathers, feathers-plugin |

## Links

- npm: https://www.npmjs.com/package/feathers-authentication-client
- Repository: https://github.com/feathersjs/feathers-authentication-client
- Issues: https://github.com/feathersjs/feathers-authentication-client/issues
- npm.io page: https://npm.io/package/feathers-authentication-client

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.2.0
- [jwt-decode](https://npm.io/package/jwt-decode.md) ^2.1.0
- [feathers-errors](https://npm.io/package/feathers-errors.md) ^2.4.0

## Alternatives

- [@fortawesome/react-fontawesome](https://npm.io/package/@fortawesome/react-fontawesome.md) — 2.2M weekly downloads
- [roboto-fontface](https://npm.io/package/roboto-fontface.md) — 196.0K weekly downloads
- [@react-native-vector-icons/common](https://npm.io/package/@react-native-vector-icons/common.md) — 150.4K weekly downloads
- [@procore/core-icons](https://npm.io/package/@procore/core-icons.md) — 4.6K weekly downloads
- [@react-md/material-icons](https://npm.io/package/@react-md/material-icons.md) — 1.6K weekly downloads

## Recent versions

- 0.3.3 (latest) — 2017-07-18
- 0.3.2 — 2017-04-30
- 0.3.1 — 2017-03-10
- 0.3.0 — 2017-03-08
- 0.2.0 — 2017-03-07
- 0.1.10 — 2017-03-03
- 0.1.9 — 2017-03-01
- 0.1.8 — 2017-02-05
- 0.1.7 — 2017-01-29
- 0.1.6 — 2016-12-14
- 0.1.4 — 2016-12-14
- 0.1.5 — 2016-12-13
- 0.1.3 — 2016-11-23
- 0.1.2 — 2016-11-22
- 0.1.1 — 2016-11-21
- … 1 more at https://npm.io/package/feathers-authentication-client/versions

## README

# feathers-authentication-client

[![Greenkeeper badge](https://badges.greenkeeper.io/feathersjs/feathers-authentication-client.svg)](https://greenkeeper.io/)

[![Build Status](https://travis-ci.org/feathersjs/feathers-authentication-client.png?branch=master)](https://travis-ci.org/feathersjs/feathers-authentication-client)
[![Code Climate](https://codeclimate.com/github/feathersjs/feathers-authentication-client/badges/gpa.svg)](https://codeclimate.com/github/feathersjs/feathers-authentication-client)
[![Test Coverage](https://codeclimate.com/github/feathersjs/feathers-authentication-client/badges/coverage.svg)](https://codeclimate.com/github/feathersjs/feathers-authentication-client/coverage)
[![Dependency Status](https://img.shields.io/david/feathersjs/feathers-authentication-client.svg?style=flat-square)](https://david-dm.org/feathersjs/feathers-authentication-client)
[![Download Status](https://img.shields.io/npm/dm/feathers-authentication-client.svg?style=flat-square)](https://www.npmjs.com/package/feathers-authentication-client)
[![Slack Status](http://slack.feathersjs.com/badge.svg)](http://slack.feathersjs.com)

> The authentication plugin for feathers-client

## Installation

```
npm install feathers-authentication-client --save
```

**Note:** This is only compatibile with `feathers-authentication@1.x` and above.

## Documentation

<!-- Please refer to the [feathers-authentication-client documentation](http://docs.feathersjs.com/) for more details. -->

## API

This module contains:

1. The main entry function
2. Some helpful hooks

The main feathers client instance has a few public methods:

- `app.authenticate(options)` - Authenticate by passing credentials.
- `app.logout()`

It also has a `app.passport` instance that, like on the server, exposes utils functions for dealing with JWTs:

- `app.passport.getJWT()` - pull it from localstorage or the cookie
- `app.passport.verifyJWT(token)` - verify that a JWT is not expired and decode it to get the payload.

**Note:** All these methods return promises.

### Handling the special re-authentication errors

In the event that your server goes down or the client loses connectivity, it will automatically handle attempting to re-authenticate the socket when the client regains connectivity with the server. In order to handle an authentication failure during automatic re-authentication you need to implement the following event listener:

```js
const errorHandler = error => {
  app.authenticate({
    strategy: 'local',
    email: 'admin@feathersjs.com',
    password: 'admin'
  }).then(response => {
    // You are now authenticated again
  });
};

// Handle when auth fails during a reconnect or a transport upgrade
app.on('reauthentication-error', errorHandler)
```


### Default Options

The following default options will be mixed in with the settings you pass in when configuring authentication. It will set the mixed options back to to the app so that they are available at any time by `app.get('auth')`. They can all be overridden.

```js
{
  header: 'Authorization', // the default authorization header
  path: '/authentication', // the server side authentication service path
  jwtStrategy: 'jwt', // the name of the JWT authentication strategy 
  entity: 'user', // the entity you are authenticating (ie. a users)
  service: 'users', // the service to look up the entity
  cookie: 'feathers-jwt', // the name of the cookie to parse the JWT from when cookies are enabled server side
  storageKey: 'feathers-jwt', // the key to store the accessToken in localstorage or AsyncStorage on React Native
}
```

### Hooks

There are 3 hooks. They are really meant for internal use and you shouldn't need to worry about them very often.

1. `populateAccessToken` - Takes the token and puts in on `hooks.params.accessToken` in case you need it in one of your client side services or hooks
2. `populateHeader` - Add the accessToken to the authorization header
3. `populateEntity` - Experimental. Populate an entity based on the JWT payload.

## Complete Example

Here's an example of a Feathers client that uses `feathers-authentication-client`. 

```js
const feathers = require('feathers/client');
const rest = require('feathers-rest/client');
const superagent = require('superagent');
const hooks = require('feathers-hooks');
const localStorage = require('localstorage-memory');
const auth = require('feathers-authentication-client');

const client = feathers();

// NOTE: the order is important: auth must be configured _after_ rest/socket
client.configure(hooks())
  .configure(rest('http://localhost:3030').superagent(superagent))
  .configure(auth({ storage: localStorage }));

client.authenticate({
  strategy: 'local',
  email: 'admin@feathersjs.com',
  password: 'admin'
})
.then(response => {
  console.log('Authenticated!', response);
  return client.passport.verifyJWT(response.accessToken);
})
.then(payload => {
  console.log('JWT Payload', payload);
  return client.service('users').get(payload.userId);
})
.then(user => {
  client.set('user', user);
  console.log('User', client.get('user'));
})
.catch(function(error){
  console.error('Error authenticating!', error);
});
```

## License

Copyright (c) 2016

Licensed under the [MIT license](LICENSE).

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