1 thought on “How to keep a user logged in

  1. Yeah this should work as default. You do however need to check for this yourself when the app boots. I do something like so in the “first step component” of my app (cut down a lot):

    class App extends React.Component {
    
      constructor() {
        super();
        this.state = {
          loading: true,
          authenticated: false,
        };
      }
    
      componentDidMount() {
        firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            this.setState({ loading: false, authenticated: true });
          } else {
            this.setState({ loading: false, authenticated: false });
          }
        });
      }
    
      render() {
        if (this.state.loading) return null; // Render loading/splash screen etc
    
        if (!this.state.authenticated) {
          return <Login />;
        }
    
        return <Home />;
      }
    }

Comments are closed.

2 thoughts on “How to keep a user logged in

  1. Yeah this should work as default. You do however need to check for this yourself when the app boots. I do something like so in the “first step component” of my app (cut down a lot):

    class App extends React.Component {
    
      constructor() {
        super();
        this.state = {
          loading: true,
          authenticated: false,
        };
      }
    
      componentDidMount() {
        firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            this.setState({ loading: false, authenticated: true });
          } else {
            this.setState({ loading: false, authenticated: false });
          }
        });
      }
    
      render() {
        if (this.state.loading) return null; // Render loading/splash screen etc
    
        if (!this.state.authenticated) {
          return <Login />;
        }
    
        return <Home />;
      }
    }

Comments are closed.

1 thought on “How to keep a user logged in

  1. Yeah this should work as default. You do however need to check for this yourself when the app boots. I do something like so in the “first step component” of my app (cut down a lot):

    class App extends React.Component {
    
      constructor() {
        super();
        this.state = {
          loading: true,
          authenticated: false,
        };
      }
    
      componentDidMount() {
        firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            this.setState({ loading: false, authenticated: true });
          } else {
            this.setState({ loading: false, authenticated: false });
          }
        });
      }
    
      render() {
        if (this.state.loading) return null; // Render loading/splash screen etc
    
        if (!this.state.authenticated) {
          return <Login />;
        }
    
        return <Home />;
      }
    }

Comments are closed.