Comments (10)
I took a stab at combining the LocalStorage and AsyncStorage codebases using React's built-in platform extensions (https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions). The example is in my forked repo. Realistically, all it does currently is import either LocalStorage or AsyncStorage based on platform, and alias' both with the same name to make the actual index.ts import a single line.
Unfortunately, this would also mean that anything depending on LocalStorage currently (which would be around 3/4's of the code) would need to be async. In my linked repo I've taken care of this, so that you can see what exactly would have to be updated. That being said, I'm significantly more in favor of this approach as opposed to a completely separate fork that shares most of its code with the (still maintained) upstream repo.
EDIT: Editting to address the JWT library. I completely missed that the current library isn't RN compatible. I think that the library @mvanroon suggested is a good drop-in replacement; I'll add that to what I have when I have the time.
from axios-jwt.
@stereodenis not sure where your error came from, but this library uses localstorage and is therefore currently incompatible with react native
You could make a PR or fork this library. Then use AsyncStorage for storing the tokens and this react-native compatible library to decode them.
from axios-jwt.
We're not married to localStorage
from axios-jwt.
We're not married to localStorage
There’s no cross-platform (web and React native) solution for local storage. In order to make this library RN-compatible, we’d have to implement a check to see what environment the code is running on and use React Native’s AsyncStorage instead of window.localStorage if the answer is react-native.
I think this can be done with navigator?.product === ‘ReactNative’
as suggested here (haven’t tried this yet):
https://stackoverflow.com/a/39473604/946872
Additionally we may have to switch to the jwt-decode library to decode the tokens.
from axios-jwt.
I've forked this library and plan to publish it as react-native-axios-jwt: https://github.com/mvanroon/react-native-axios-jwt/
The reason why I went with a fork instead of a pull request:
- It's clearer for developers which library to use (web project? axios-jwt, react-native project? react-native-axios-jwt)
- React-native uses AsyncStorage where storing retrieving is done with async functions. This means that pretty much every exported function from this library becomes an async one (breaking changes)
from axios-jwt.
I don't see anything wrong with making everything an async function. Just call it v2.0.0
from axios-jwt.
I think recombining both libraries would quickly result in messy code (if-elsing between both storage solutions). But feel free to take a stab at it.
from axios-jwt.
I think recombining both libraries would quickly result in messy code (if-elsing between both storage solutions). But feel free to take a stab at it.
Just DI the storage instead of hard coding it. Provide an interface for tokens storage.
from axios-jwt.
I think recombining both libraries would quickly result in messy code (if-elsing between both storage solutions). But feel free to take a stab at it.
Just DI the storage instead of hard coding it. Provide an interface for tokens storage.
that's a great idea! - maybe have it default to localstorage so it can be omitted
from axios-jwt.
I went with a slightly different approach: #29
Let me know what you think!
from axios-jwt.
Related Issues (20)
- Support rotating refresh tokens
- Breaks on webpack 5 HOT 5
- Retry failed request after RefreshToken HOT 2
- SECURITY: Token should not be stored in the localstorage to limit XSS vulnerability HOT 9
- Breaks on axios > 0.21.4
- Allow a different storage. HOT 9
- Version 2 breaks react 18 support
- localstorage is not defined HOT 2
- 'getStorage' does not exist in type 'IAuthTokenInterceptorConfig' HOT 3
- undefined window and Synchronous isLoggedIn HOT 4
- Uncaught TypeError: (0 , ms_1.default) is not a function HOT 6
- No requests are made if applyAuthTokenInterceptor called HOT 1
- StorageType undefined outside the interceptor? HOT 1
- Feature Request: Allow prepended string for token names
- not setting header HOT 4
- Use a less strict peer dependency for @react-native-async-storage/async-storage HOT 1
- No dist/ folder from clean install HOT 2
- Bug in README.md JavaScript Version HOT 2
- Question HOT 6
- getAccessToken() returns undefined HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios-jwt.