reindex-js
is a client library that makes using Reindex
(https://www.reindex.io) easier. It provides
support for authentication, including social login. It also exposes hooks for
using Reindex with Relay and other libraries.
reindex-js
is a universal JavaScript library, so it works in the browser,
React Native and Node.js. However, log in with social providers (the login()
method) is only available in the browser. Log in with Auth0 (loginWithToken()
)
is also supported on React Native.
npm install reindex-js
Import the library
import Reindex from 'reindex-js'
Create an instance.
const reindex = new Reindex('https://YOUR-REINDEX-URL.myreindex.com');
Create a new instance of Reindex. Usually only one instance is needed so you can create it in one model and export.
import Reindex from 'reindex';
const reindex = new Reindex('https://YOUR-REINDEX-URL.myreindex.com');
export default reindex;
In browser environment, if token was stored in localStorage
, it will be
retrieved and stored inside the instance.
Browser only
Attempt to login with a providerName
(google
, facebook
, twitter
or
github
). The provider should be set up and enabled in your Reindex app.
Opens a browser window with provider's login screen, where the user needs to grant your application permissions to read their data. If everything succeeds the promise returned is resolved with an object with following properties:
token
- JSON Web Token for the user,user
- information about the user.
If the log in fails, the promise is reject with an error.
The token is stored in the Reindex
instance and in localStorage
of the browser.
Emits login
and tokenChange
events.
Attempt to login to Reindex with an Auth0
id_token
. Reindex validates the token
on the backend side and logs the user in. Like login
, sets token in the
Reindex
object and stores it in localStorage
(if available).
Emits login
and tokenChange
events.
Example usage with Auth0 Lock:
const reindex = new Reindex(reindexURL);
const lock = new Auth0Lock(auth0ClientID, auth0Domain);
lock.show((error, profile, id_token) => {
reindex.loginWithToken('auth0', id_token);
});
Browser only
Clears token from the instance and localStorage
. Resolve promise with map
of token
and user
, both of which are null.
Emits logout
and tokenChange
events.
Returns true if there is a token stored inside the instance.
Set the token manually. Can be used when custom authentication provider is used or on server.
Emits tokenChange
event.
Queries the Reindex backend. Returns Promise that resolves if HTTP request succeeded, rejects otherwise.
Returns map of header name to value that need to be used to authenticate request with Reindex backend.
Returns RelayNetworkLayer. Usage:
import Relay from 'react-relay';
import Reindex from 'reindex-js';
const reindex = new Reindex('https://YOUR-REINDEX-URL.myreindex.com');
Relay.injectNetworkLayer(reindex.getRelayNetworkLayer());
Reindex
extends EventEmitter, so it
can be (un)subscribed with on
and off
. Here are the possible events:
When login succeded. loginResponse
is the response from backend.
When logout succeded.
When token changes. token
is the new token.
To build UMD bundle:
npm run build:umd