-1

Can anyone help me understand what's going wrong here?

I'm trying to redirect to the login page after a signout.

Here's the code

import React, { Component } from "react";
import firebase from "../Firebase/firebase";
import { withRouter, Redirect } from "react-router-dom";

class Home extends Component {
  constructor(props) {
    super(props);
    this.logout = this.logout.bind(this);
  }

  state = {};

  logout = e => {
    e.preventDefault();
    firebase
      .auth()
      .signOut()
      .then(
        function() {
          console.log("Signed Out");
          this.props.history.push("/login");
        },
        function(error) {
          console.error("Sign Out Error", error);
        }
      );
  };

  render() {
    return (
      <div align="center" className="container">
        <h1> Home page </h1>
        <br />
        <br />
        <h2>Hello: {this.state.user} </h2>
        <button
          className="btn btn-lg text-white"
          style={{ backgroundColor: "#B65DF3" }}
          onClick={this.logout}
        >
          Logout
        </button>
      </div>
    );
  }
}

export default withRouter(Home);

Here's the error message

TypeError: Cannot read property 'props' of undefined

I don't understand why it's not working as my login page which redirects here is the exact same and that works.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Neil Grogan
  • 177
  • 1
  • 4
  • 9

1 Answers1

0

Your issue would be the anonymous function you pass .then. Try replacing it with fat arrows or something like:

  logout = (e) => {
        e.preventDefault();
        firebase.auth().signOut().then(this.handleResolve).catch(this.handleError)
    }

  handleResolve = () => {
    console.log('Signed Out');
    this.props.history.push('/login')
  }

  handleError = (error) => {
    console.error('Sign Out Error', error);
  }
Tom Finney
  • 2,670
  • 18
  • 12