Skip to content

PHPiotr/react-jwt-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-jwt-store

React JWT user store

Usage

const userStore = require('react-jwt-store')()

userStore.on('Token received', (token, user) => {
  console.log(token, user)
})

userStore.init()

Initialize

In order to trigger the store's refresh mechanism and send data to any event handlers, you must call the init method.

userStore.init()

Set the token

You can set the token without interacting with cookies via the following.

userStore.setToken('jwt')

Refresh the token

You can force a refresh of the token via the following.

userStore.refreshToken()

Override Cookie Key

By default, the JWT assumes the cookie key is XSRF-TOKEN. This can be overridden by passing cookie on the options hash:

  let userStore = require('react-jwt-store')({ cookie: 'NOT-XSRF-TOKEN'});

Set a logger

By default, the store does not log anything, but if you pass in a console compatible logger, the store will log the state of the token as it changes.

Terminate

By default, if you set token to null:

userStore.setToken(null)

...an interval responsible for refreshing tokens (the one set up on init) will not be cleared. If you want to clear it explicitly, then you can do it with:

userStore.terminate()

About

React JWT user store

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 7