feathers-connect
The FeathersJS client library for DoneJS and can-connect
Quick start
Install the plugin:
npm install feathers-connect --save
Instantiate a Feathers
instance for each Feathers API server:
; const feathers = ; ;
Use it in your can-connect model:
;;;; // Import the feathers instance.; const Message = canMap; MessageList = canList; const messageConnection = ; ; // Connect to realtime events.feathersio;feathersio;feathersio;feathersio; ;
Your Message
model is ready to go with all real-time features in place.
Usage
See the usage page.
Authentication
Feathers requires a JWT token in the Authorization header to authenticate requests. This package includes methods to assist with token management. The feathers.authenticate()
method persists the token to a storage engine. Request made through the feathers.rest()
helper automatically look for the token on the storage engine. The feathers.logout()
method removes the token from storage. Here is an example session model. The magic happens in the sessionConnection
.
/* global window */ ;;;; ;;;;;;;;;;;;;; var behaviors = 'constructor' 'can-map' 'constructor-store' 'data-callbacks' 'data-combine-requests' 'data-inline-cache' 'data-parse' 'data-url' 'constructor-callbacks-once'; const Session = canMap; SessionList = canList; const sessionConnection = ; ;
API
feathers.authenticate(data)
- Can authenticate using either thetokenEndpoint
or thelocalEndpoint
. (see the Configuration section, below).token
authentication is the default, so callingfeathers.authenticate()
with no options will attempt to find the token in the storage engine and will send it with the request. If the token is valid, the Feathers server will return a fresh token.
To authenticate with username and password, pass in an object of this format:
feathers;
feathers.rest(location, idProp)
- This is used to configure can-connect'surl
behavior. Thelocation
is required, butidProp
is optional if the service uses the defaultidProp
. (see the configuration options in the Configuration section.)feathers.logout()
- This simply removes the token from storage. It's actually synchronous, but returns a promise if you prefer to use one.
Configuration
When instantiating a Feathers
instance, you can pass a configuration object to the constructor. For most applications, the only options that will need to be specified will be the url
and the idProp
.
; const feathers = // The current server is assumed to be the API server. url: '' // Determines if the token is persisted to the `storage` provider. storeToken: true // The storage engine used to persist the token on the client. storage: cookieStorage // The key name of the location where the token will be stored. tokenLocation: 'ssr-cookie' // The default `idProp` for all services. idProp: 'id' // The endpoint for token authentication. tokenEndpoint: 'auth/token' // The endpoint for username/password authentication. localEndpoint: 'auth/local' // Store the token in a cookie for SSR by default. ssr: true; ;
url
- This is the base url of the Feathers server with no trailing slash. The default setting assumes that the Feathers and SSR servers are at the same url.storeToken
- A boolean that determines if the token gets persisted to the providedstorage
engine. To truly turn off token storage, you also need to set thessr
option tofalse
. Note: This will make it so that the user has to login again after every refresh.storage
- You can provide your own Web Storage-compatible storage engine. The default storage engine is a cookie-storage instance. Cookies, when used correctly to prevent CSRF attacks, are preferred over localStorage for a couple of reasons. (1) The SSR server needs to be able to access the token on first load. This is only possible with a cookie. Because the Feathers does not consume the cookie, we don't have to worry about CSRF access to the API server. It's common to enable CORS for the API server, but make sure you disable CORS requests to the SSR server. (2) localStorage doesn't work in private mode on all browsers. By using a cookie, the user will be able to still refresh the browser and the cookie will be deleted when the private browsing session ends.tokenLocation
- this is the key name of the location in thestorage
engine. The default isssr-cookie
, which is the same default cookie name used by the[feathers-done-ssr](https://www.npmjs.com/package/feathers-done-ssr)
package. The values will need to match on both packages in order for the SSR server to send authenticated requests on behalf of the user.idProp
- This is the key name of theid
property to be used on all services. For example, if your Feathers services mostly all use MongoDB, then set this to_id
. This can also be customized on a per-service basis. This means that if most of your services use PostgreSQL, the defaultidProp
ofid
will work for those services, but if you have a single NeDB, Mongoose, or MongoDB service, you can specify a differentidProp
in thefeathers.rest()
method.tokenEndpoint
- The endpoint for token authentication. It needs to match the service location configured on the Feathers server.localEndpoint
- The endpoint for username/password authentication. It needs to match the service location configured on the Feathers server.ssr
- You can set this to false to prevent the token from being stored in an SSR cookie. Setting bothssr
andstoreToken
to false will disable token storage completely.
Contributing
Making a Build
To make a build of the distributables into dist/
in the cloned repository run
npm install
node build
Running the tests
Tests can run in the browser by opening a webserver and visiting the test.html
page.
Automated tests that run the tests from the command line in Firefox can be run with
npm test