Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅在google身份验证回调后运行保存用户函数_Javascript_Reactjs_Firebase_Firebase Authentication - Fatal编程技术网

Javascript 仅在google身份验证回调后运行保存用户函数

Javascript 仅在google身份验证回调后运行保存用户函数,javascript,reactjs,firebase,firebase-authentication,Javascript,Reactjs,Firebase,Firebase Authentication,我已经在React+Firebase中构建了一个google身份验证。我成功地添加了此功能,但目前每次他们回家时,此功能都会调用firebase.auth().getRedirectResult()。我只想在google身份验证回调后调用此 class Home extends Component { constructor(props) { super(props); this.state = { user: null }; } async

我已经在React+Firebase中构建了一个google身份验证。我成功地添加了此功能,但目前每次他们回家时,此功能都会调用
firebase.auth().getRedirectResult()
。我只想在google身份验证回调后调用此

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

    this.state = {
      user: null
    };
  }

  async componentWillMount() {
    firebase.auth().onAuthStateChanged(user => {
      this.setState({ user });

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

    // Want to call this only after google authentication callback
    firebase
      .auth()
      .getRedirectResult()
      .then(result => {
        if (result.credential) {
          var token = result.credential.accessToken;
          console.log("token", token);
        }
        var user = result.user;
        this.props.createUser(user);
      })
      .catch(function(error) {
        console.log("error", error);
      });

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

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

function mapStateToProps({ user }) {
  return { user: user };
}
class Home扩展组件{
建造师(道具){
超级(道具);
此.state={
用户:空
};
}
异步组件willmount(){
firebase.auth().onAuthStateChanged(用户=>{
this.setState({user});
如果(用户){
this.props.history.push(“/user/settings”);
}
});
//只想在google身份验证回调后调用它吗
火基
.auth()
.getRedirectResult()
。然后(结果=>{
if(结果凭证){
var token=result.credential.accessToken;
日志(“令牌”,令牌);
}
var user=result.user;
this.props.createUser(用户);
})
.catch(函数(错误){
console.log(“错误”,error);
});
onLogin=()=>{
const provider=new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(提供程序);
};
render(){
返回(
   // 登录谷歌
谷歌登录
);
}
}
函数MapStateTops({user}){
返回{user:user};
}
我将
firebase.auth().getRedirectResult()
设置为
if(user){}

像这样:

    firebase.auth().onAuthStateChanged(user => {
      this.setState({ user });

      if (user) {
        this.props.history.push("/user/settings");
    firebase
      .auth()
      .getRedirectResult()
      .then(result => {
        if (result.credential) {
          var token = result.credential.accessToken;
          console.log("token", token);
        }
        var user = result.user;
        this.props.createUser(user);
      })
      .catch(function(error) {
        console.log("error", error);
      });
      }
    });

你如何表明你在谷歌登录后被重定向?你是否可以利用查询字符串参数?或者说如果没有设置
user
,那么运行代码是否公平?通过单击
onLogin
向谷歌验证。然后你可以看到谷歌登录页面。此应用注册
createUser(user)
(这是Redux代码,可以正常工作)。问题是登录页面和回调页面位于同一个组件中……我不希望仅访问此页面的用户运行
firebase.auth().getRedirectResult()。