使用Google Auth登录后的Redux问题

问题描述 投票:0回答:1

我正在尝试对我的应用程序实施Google OAuth。当我单击创建的“使用Google登录”按钮时,Google登录会弹出。但是之后,我在Redux devtools中看不到任何更改,它将无法登录

当我在浏览器的控制台中输入以下代码(例如gapi.auth2.getAuthInstance().signIn())时,我的代码有效,然后我的按钮将从“使用google登录”更改为“退出”。而且我看到了对它可以工作的redux开发工具的更改。但由于某些原因,当单击我的应用程序的按钮时,它将无法使用。

GoogLeAuth.js

import React, { Component } from "react";
import { connect } from "react-redux";
import { signIn, signOut } from "./actions/index";

class GoogleAuth extends Component {
  componentDidMount() {
    window.gapi.load("client:auth2", () => {
      window.gapi.client
        .init({
          clientId:
            "ID",
          scope: "email"
        })
        .then(() => {
          this.auth = window.gapi.auth2.getAuthInstance();
          this.onAuthChange(this.auth.isSignedIn.get());
          this.auth.isSignedIn.listen(this.onAuthChange);
        });
    });
  }

  onAuthChange = isSignedIn => {
    if (isSignedIn) {
      this.props.signIn(this.auth.currentUser.get().getId());
    } else {
      this.props.signOut();
    }
  };
  onSignInClick = () => {
    this.auth.signIn();
  };

  onSignOutClick = () => {
    this.auth.signOut();
  };

  authButton = () => {
    if (this.props.isSignedIn === null) {
      return null;
    } else if (this.props.isSignedIn) {
      return <button onClick={this.onSignOutClick}>Sign Out</button>;
    } else {
      return <button onClick={this.onSignInClick}>Sign In with Google</button>;
    }
  };

  render() {
    return <div>{this.authButton()}</div>;
  }
}

const mapStateToProps = state => {
  return { isSignedIn: state.auth.isSignedIn };
};

export default connect(
  mapStateToProps,
  { signIn, signOut }
)(GoogleAuth);
javascript reactjs redux google-authentication
1个回答
0
投票

您还需要在signIn中调用onSignInClick操作:

onSignInClick = () => {
    this.auth.signIn();
    this.props.signIn();
};

signOut中的onSignOutClick相同。

© www.soinside.com 2019 - 2024. All rights reserved.