Giter Site home page Giter Site logo

[React Native] Not working about axios-jwt HOT 10 OPEN

jetbridge avatar jetbridge commented on August 23, 2024
[React Native] Not working

from axios-jwt.

Comments (10)

suniahk avatar suniahk commented on August 23, 2024 1

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.

mvanroon avatar mvanroon commented on August 23, 2024

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

revmischa avatar revmischa commented on August 23, 2024

We're not married to localStorage

from axios-jwt.

mvanroon avatar mvanroon commented on August 23, 2024

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.

mvanroon avatar mvanroon commented on August 23, 2024

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.

revmischa avatar revmischa commented on August 23, 2024

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

from axios-jwt.

mvanroon avatar mvanroon commented on August 23, 2024

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.

magom001 avatar magom001 commented on August 23, 2024

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.

mvanroon avatar mvanroon commented on August 23, 2024

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.

mvanroon avatar mvanroon commented on August 23, 2024

I went with a slightly different approach: #29
Let me know what you think!

from axios-jwt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.