Code Monkey home page Code Monkey logo

Comments (5)

nk2580 avatar nk2580 commented on June 11, 2024

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?

from react-firebase.

Ahmdrza avatar Ahmdrza commented on June 11, 2024

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

from react-firebase.

rohan-deshpande avatar rohan-deshpande commented on June 11, 2024

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

from react-firebase.

rohan-deshpande avatar rohan-deshpande commented on June 11, 2024

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

from react-firebase.

rohan-deshpande avatar rohan-deshpande commented on June 11, 2024

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.

from react-firebase.

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.