axios-jwt icon indicating copy to clipboard operation
axios-jwt copied to clipboard

[React Native] Not working

Open stereodenis opened this issue 4 years ago • 10 comments

Unable to resolve module `stream` from `node_modules/jws/lib/sign-stream.js`: stream could not be found within the project

stereodenis avatar Feb 15 '21 14:02 stereodenis

@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.

mvanroon avatar Apr 02 '21 06:04 mvanroon

We're not married to localStorage

revmischa avatar Apr 02 '21 08:04 revmischa

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.

mvanroon avatar Apr 03 '21 08:04 mvanroon

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)

mvanroon avatar Apr 10 '21 18:04 mvanroon

I don't see anything wrong with making everything an async function. Just call it v2.0.0

revmischa avatar Apr 10 '21 18:04 revmischa

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.

mvanroon avatar Apr 10 '21 18:04 mvanroon

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.

suniahk avatar Jun 18 '21 00:06 suniahk

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.

magom001 avatar Jun 22 '21 13:06 magom001

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

mvanroon avatar Jun 22 '21 14:06 mvanroon

I went with a slightly different approach: https://github.com/jetbridge/axios-jwt/pull/29 Let me know what you think!

mvanroon avatar Nov 08 '21 10:11 mvanroon