Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Feathers authentication 1.0 compatible client #7

Merged
merged 12 commits into from
Nov 18, 2016
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ node_modules
.lock-wscript

# The compiled/babelified modules
lib/
lib/

# Yarn lockfile
yarn.lock
84 changes: 71 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,88 @@ npm install feathers-authentication-client --save

## Documentation

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

### 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
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 server that uses `feathers-authentication-client`.

```js
const feathers = require('feathers');
const rest = require('feathers-rest');
const feathers = require('feathers/client');
const rest = require('feathers-rest/client');
const superagent = require('superagent');
const hooks = require('feathers-hooks');
const bodyParser = require('body-parser');
const errorHandler = require('feathers-errors/handler');
const localStorage = require('localstorage-memory');
const auth = require('feathers-authentication-client');

// Initialize the application
const app = feathers()
.configure(rest())
.configure(hooks())
.configure(auth())
.use(errorHandler());
const client = feathers();

app.listen(3030);
client.configure(hooks())
.configure(rest('http://localhost:3030').superagent(superagent))
.configure(auth({ storage: localStorage }));

console.log('Feathers app started on 127.0.0.1:3030');
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.id);
})
.then(user => {
client.set('user', user);
console.log('User', client.get('user'));
})
.catch(function(error){
console.error('Error authenticating!', error);
});
```

## License
Expand Down
73 changes: 73 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const feathers = require('feathers');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const primus = require('feathers-primus');
const hooks = require('feathers-hooks');
const memory = require('feathers-memory');
const bodyParser = require('body-parser');
const errors = require('feathers-errors');
const errorHandler = require('feathers-errors/handler');
const auth = require('feathers-authentication');
const local = require('feathers-authentication-local');
const jwt = require('feathers-authentication-jwt');

function customizeJWTPayload() {
return function(hook) {
hook.data.payload = {
id: hook.params.user.id
};

return Promise.resolve(hook);
};
}

const app = feathers();
app.configure(rest())
.configure(socketio())
// .configure(primus({ transformer: 'websockets' }))
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'supersecret' }))
.configure(local())
.configure(jwt())
.use('/users', memory())
.use('/', feathers.static(__dirname + '/public'))
.use(errorHandler());

app.service('authentication').hooks({
before: {
create: [
// You can chain multiple strategies
auth.hooks.authenticate(['jwt', 'local']),
customizeJWTPayload()
],
remove: [
auth.hooks.authenticate('jwt')
]
}
});

// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
app.service('users').hooks({
before: {
find: auth.hooks.authenticate('jwt'),
get: auth.hooks.authenticate('jwt'),
create: local.hooks.hashPassword({ passwordField: 'password' })
}
});

var User = {
email: 'admin@feathersjs.com',
password: 'admin',
permissions: ['*']
};

app.service('users').create(User).then(user => {
console.log('Created default user', user);
}).catch(console.error);

app.listen(3030);

console.log('Feathers authentication with local auth started on 127.0.0.1:3030');
44 changes: 44 additions & 0 deletions example/primus-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// NOTE (EK): You need to uncomment the primus setup
// and comment out the socket.io setup inside app.js

const feathers = require('feathers/client');
const primus = require('feathers-primus/client');
const hooks = require('feathers-hooks');
const localStorage = require('localstorage-memory');
const Primus = require('primus');
const Emitter = require('primus-emitter');
const auth = require('../lib/index');

const Socket = Primus.createSocket({
transformer: 'websockets',
plugin: {
'emitter': Emitter
}
});
const socket = new Socket('http://localhost:3030');
const client = feathers();

client.configure(hooks())
.configure(primus(socket, { timeout: 1000 }))
.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.id);
})
.then(user => {
client.set('user', user);
console.log('User', client.get('user'));
})
.catch(function(error){
console.error('Error authenticating!', error);
});
33 changes: 33 additions & 0 deletions example/rest-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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('../lib/index');

const client = feathers();

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.id);
})
.then(user => {
client.set('user', user);
console.log('User', client.get('user'));
})
.catch(function(error){
console.error('Error authenticating!', error);
});
34 changes: 34 additions & 0 deletions example/socketio-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const feathers = require('feathers/client');
const socketio = require('feathers-socketio/client');
const hooks = require('feathers-hooks');
const localStorage = require('localstorage-memory');
const io = require('socket.io-client');
const auth = require('../lib/index');

const socket = io('http://localhost:3030');
const client = feathers();

client.configure(hooks())
.configure(socketio(socket))
.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.id);
})
.then(user => {
client.set('user', user);
console.log('User', client.get('user'));
})
.catch(function(error){
console.error('Error authenticating!', error);
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"feathers": "^2.0.2",
"feathers-authentication": "^0.8.0-beta-2",
"feathers-hooks": "^1.5.8",
"feathers-authentication": "^1.0.0-beta",
"feathers-authentication-jwt": "^0.1.0",
"feathers-authentication-local": "^0.2.0",
"feathers-hooks": "^1.6.0",
"feathers-memory": "^0.8.0",
"feathers-primus": "^1.4.1",
"feathers-rest": "^1.5.1",
Expand All @@ -73,10 +75,10 @@
"mocha": "^3.1.2",
"primus": "^6.0.3",
"primus-emitter": "^3.1.1",
"request": "^2.75.0",
"rimraf": "^2.5.4",
"semistandard": "^9.1.0",
"socket.io-client": "^1.5.0",
"superagent": "^2.3.0",
"ws": "^1.1.1"
}
}
Loading