Coder Social home page Coder Social logo

Comments (5)

mvanroon avatar mvanroon commented on May 13, 2024

I don’t see how this library has anything to do with react navigation or conditional rendering.

from react-native-axios-jwt.

ericlifs avatar ericlifs commented on May 13, 2024

Thanks for this awesome tool! I'm facing the same issue @c0nsoleg tho: there is no way to react to when the user gets logged in or logged out in order to do something like:

const isLoggedIn = useIsUserLoggedIn();

if (isLoggedIn) {
  return <HomeNavigation />;
}

return <AuthNavigation />;

You can answer with "this library is just for setting the axios headers interceptors" and you would be 100% right, but at the same time it would be nice to have the possibility to respond accordingly.

Thanks!

from react-native-axios-jwt.

mvanroon avatar mvanroon commented on May 13, 2024

Hi @ericlifs, have you tried using the isLoggedIn function that is exported from this library? Check out the readme to learn how to use this library.

from react-native-axios-jwt.

ericlifs avatar ericlifs commented on May 13, 2024

Hey @mvanroon, how are you? Yes, I've tried using that isLoggedIn function but it doesn't fit our requirement as it is an async function that returns a boolean indicating whether the user is logged at the time of calling the function or not. We were talking about having a hook that returns a boolean indicating when the headers are set or not, by doing that you can react to changes on this boolean and show different navigators accordingly.

Also, there is a "bug" in the readme file: this is the code for the isLoggedIn function:

/**
 * Checks if refresh tokens are stored
 * @async
 * @returns {Promise<boolean>} Whether the user is logged in or not
 */
export const isLoggedIn = async (): Promise<boolean> => {
  const token = await getRefreshToken()
  return !!token
}

If you do in your code:

if (isLoggedIn()) {
  console.log('logged in')
} else {
  console.log('not logged in')
}

It will always show 'logged in' because the isLoggedIn function is an async function and therefore returns a Promise<boolean> which is always true (you'll always need to await that call).

Eric.

from react-native-axios-jwt.

mvanroon avatar mvanroon commented on May 13, 2024

You’re right! Looks like the readme needs to be updated. Could you open a PR?

As for ‘caching’ the auth state. You could build this in your own project or we could create a hook that returns the logged in state. This state would need to be updated when a user logs in or out.

Wanna take a stab at it? I’m available for reviewing pull requests.

from react-native-axios-jwt.

Related Issues (9)

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.