Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

feathers-authentication-client

feathersjs3.7kMITdeprecated0.3.3TypeScript support: included

Feathers v3 is out and this module has moved to @feathersjs/authentication-client. See https://docs.feathersjs.com/migrating.html for more information.

The authentication plugin for feathers-client

feathers, feathers-plugin

readme

feathers-authentication-client

Greenkeeper badge

Build Status Code Climate Test Coverage Dependency Status Download Status Slack Status

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

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:

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.

{
  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.

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.

changelog

Change Log

v0.3.2 (2017-04-30)

Full Changelog

Closed issues:

  • An in-range update of feathers-errors is breaking the build 🚨 #45
  • Proper way to save jwt in cookies #41
  • Allow customizing the tokenField #38
  • Show blank page in safari@iOS 8.3 #37

Merged pull requests:

v0.3.1 (2017-03-10)

Full Changelog

Closed issues:

  • The latest tag on NPM is wrong #35
  • exp claim should be optional #33

Merged pull requests:

v0.3.0 (2017-03-08)

Full Changelog

v0.2.0 (2017-03-07)

Full Changelog

Closed issues:

  • Support authenticated and logout client side events #29
  • The default header mismatches the default feathers-authentication header #23
  • Re-authenticating fails when passing options #22
  • Socket.io timeout does nothing when there is JWT token available #19

Merged pull requests:

  • Fix header casing #32 (daffl)
  • Add client side authenticated and logout events #31 (daffl)
  • Add support for socket timeouts and some refactoring #30 (daffl)

v0.1.10 (2017-03-03)

Full Changelog

Merged pull requests:

  • Remove hardcoded values for Config and Credentials typings #28 (myknbani)

v0.1.9 (2017-03-01)

Full Changelog

Merged pull requests:

v0.1.8 (2017-02-05)

Full Changelog

Closed issues:

  • Uncaught TypeError: Cannot read property 'options' of undefined #26
  • Browser Version #24

Merged pull requests:

  • Hoist upgrade handler into current scope by using an arrow function #27 (daffl)

v0.1.7 (2017-01-29)

Full Changelog

Closed issues:

  • [Webpack] TypeError: _this4.storage.getItem is not a function #18
  • [Feature request] Signup via socket #17
  • Missing auth token when used with feathers-rest in comparison to feathers-socketio #16
  • Cannot read property 'on' of undefined - feathers-authentication-client #12

Merged pull requests:

v0.1.6 (2016-12-14)

Full Changelog

Closed issues:

  • logout\(\) doesn't resolve #10

Merged pull requests:

v0.1.5 (2016-12-13)

Full Changelog

v0.1.4 (2016-12-13)

Full Changelog

Closed issues:

  • populateAccessToken tries to access non-existent property #11
  • Socket client should automatically auth on reconnect #2

Merged pull requests:

v0.1.3 (2016-11-23)

Full Changelog

Closed issues:

  • Client should ensure socket.io upgrade is complete before authenticating #4

Merged pull requests:

v0.1.2 (2016-11-22)

Full Changelog

Merged pull requests:

v0.1.1 (2016-11-21)

Full Changelog

Merged pull requests:

v0.1.0 (2016-11-18)

Closed issues:

  • Relation with feathers-authentication #6
  • Client: Docs for getJWT & verifyJWT #1

Merged pull requests:

  • Feathers authentication 1.0 compatible client #7 (ekryski)

* This Change Log was automatically generated by github_changelog_generator