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

How to know if data is loading to display loading screen rather than login page? #39

Open
Ahmdrza opened this issue Feb 11, 2020 · 5 comments

Comments

@Ahmdrza
Copy link

Ahmdrza commented Feb 11, 2020

I need to display loading screen rather than displaying login screen while data is being fetched. Can you please tell me about it? I have checked docs but I didn't find anything. Thanks.

@nk2580
Copy link

nk2580 commented Mar 9, 2020

the issue here appears to be the authState doesn't have a loading value. its probably no that difficult to add one in. did you want to work on a PR?

@Ahmdrza
Copy link
Author

Ahmdrza commented Mar 9, 2020

Yes I can work on it. But I don't get much free time so i'll be a slow a bit.

@rohan-deshpande
Copy link

rohan-deshpande commented Feb 2, 2021

Just hit this issue and it's... not ideal. Seems like there is a semi official Google maintained React bindings lib here https://github.com/FirebaseExtended/reactfire which supports loading states but the docs are SO Google. Seems unfinished and hard to follow.

For now one way around it is to store the current user in local storage and simply redirect away from the login page if they exist there. Not the best solution, but better than rendering confusing UI in my opinion until this is resolved.

Edit: seems like @rakannimer is using https://github.com/csfrequency/react-firebase-hooks in his NextJs demo app which has a loading state provided through the useAuthState hook so it might be good to check that out as well

@rohan-deshpande
Copy link

As a possible solution I created my own AuthProvider component which passes loading to children via render props

import { Component } from 'react';
import firebase from 'firebase/app';
import { func } from 'prop-types';

export class AuthProvider extends Component {
  state = {
    loading: firebase.auth().currentUser === null,
    user: firebase.auth().currentUser,
  };

  componentDidMount() {
    firebase
      .auth()
      .onAuthStateChanged(
        user => user && this.setState({ user, loading: false })
      );
  }

  render() {
    const { loading, user } = this.state;

    return this.props.children({ loading, user });
  }
}

AuthProvider.propTypes = {
  children: func,
};

That being said, I've migrated to using react-firebase-hooks as it seems to be a little more active

@rohan-deshpande
Copy link

Just found this and it lines up with my own observations - it's impossible to know if the user is logged in instantly without using local storage https://stackoverflow.com/a/51856627/12128359. The firebase.auth().currentUser method is not instant, I guess it makes some sort of async call.

Probably an okay solution would be to wire in ways to store the user in local storage within a provider like this. That way you can easily mutate the value in local storage in the onAuthStateChanged observer callback and have it be accurate.

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