Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect with jsx #14

Closed
c0nsoleg opened this issue Jan 11, 2022 · 5 comments
Closed

Connect with jsx #14

c0nsoleg opened this issue Jan 11, 2022 · 5 comments

Comments

@c0nsoleg
Copy link

Any success in using it with libraries like react-navigation and make use of isLoggedIn to conditional render something?

@mvanroon
Copy link
Owner

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

@ericlifs
Copy link

ericlifs commented Nov 12, 2022

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!

@mvanroon
Copy link
Owner

mvanroon commented Nov 13, 2022

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.

@ericlifs
Copy link

ericlifs commented Nov 13, 2022

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.

@mvanroon
Copy link
Owner

mvanroon commented Nov 13, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants