Probably by routine or by Stack Overflow syndrome, we often use a JSON Web Token(JWT) to manage this authentication between our frontend apps and their API. For convenience, we store this token in the browser's localStorage. But this is not a good practice, as Randall Degges explains in his article "Please Stop Using Local Storage". For the most curious, here is an example of how "Stealing JWTs in localStorage via XSS".
But then, how to use a JWT to manage authentication in a more secure way? ra-in-memory-jwt
is an implementation of a solution proposed by the Hasura team in their article The Ultimate Guide to handling JWTs on frontend clients.
You can find a detailed explanation of this implementation on the blog post Handling JWT in Admin Apps the Right Way.
npm install ra-in-memory-jwt
The use of ra-in-memory-jwt
is strongly linked to your API. Rather than using the npm package and the configuration options (see next part), you will probably save time to recreate the innMemoryJWT.js
file from the original file. And it will be one less dependency for your project!
ra-in-memory-jwt
must know the API endpoints to refresh the JWT. The default value is /refresh-token
, but you can change it with the setRefreshTokenEndpoint
method:
inMemoryJWT.setRefreshTokenEndpoint('http://localhost:8001/another/refresh-token-endpoint');
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
To learn more about the contributions to this project, consult the contribution guide.
ra-in-memory-jwt is licensed under the MIT License, courtesy of Marmelab.