J'essaie de créer l'authentification Google dans React + Redux + Firebase. Et je veux créer un utilisateur après l'authentification google. Mais this.props.createUser() ne fonctionne pas après les redirections sur Firebase.

TypeError: Impossible de lire la propriété 'props' d'undefined

app / src / components / home.js

import React, { Component } from "react";
import { connect } from "react-redux";
import firebase from "../config/firebase";
import { createUser } from "../actions";

class Home extends Component {
  constructor(props) {
    super(props);

    this.state = {
      user: null
    };
  }

  async componentWillMount() {
    console.log("this", this.props);
    firebase.auth().onAuthStateChanged(user => {
      this.setState({ user });

      if (user) {
        this.props.history.push("/user/settings");
      }
    });

    firebase
      .auth()
      .getRedirectResult()
      .then(function(result) {
        if (result.credential) {
          var token = result.credential.accessToken;
          console.log("token", token);
        }
        var user = result.user;
        // Successfully got a user but type error in below
        this.props.createUser(user);
      })
      .catch(function(error) {
        console.log("error", error);
      });
  }

  onLogin = () => {
    const provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithRedirect(provider);
  };

  render() {
    return (
      <button className="btn btnPrimary" onClick={this.onLogin}>
        <span>Google Signin</span>
      </button>
    );
  }
}

function mapStateToProps({ user }) {
  return { user: user };
}

export default connect(mapStateToProps, { createUser })(Home);

app / src / actions / index.js

import firebase from "../config/firebase";

export function createUser(user) {
  console.log('user', user)
}
0
k10a 14 janv. 2020 à 05:37