简体   繁体   English

如何在不使用弹出窗口的情况下使用 React Native 和 Firebase 登录 Google?

[英]How can I login to Google with React Native and Firebase without using a popup?

As I said, I want to login to my Google Account without having to create a popup, I mean keeping that Login Form with those inputs so the screen on the emulator won't be so empty.正如我所说,我想登录到我的 Google 帐户而不必创建弹出窗口,我的意思是让登录表单与这些输入保持一致,这样模拟器上的屏幕就不会那么空了。 (sorry for the image looking like that, I do not know how to edit it in here.) (抱歉图片看起来像这样,我不知道如何在这里编辑它。) 在此处输入图像描述

I will copy and paste the code for the Firebase login:我将复制并粘贴 Firebase 登录的代码:

  onButtonPress() {
    const {email, password} = this.state;

    this.setState({ error: '', loading: true });

    firebase.auth().signInWithEmailAndPassword(email, password)
    .then(this.onLoginSuccess.bind(this))
    .catch(() => {
      firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(this.onLoginSuccess.bind(this))
        .catch(this.onLoginFail.bind(this));
  })
}

But at the moment it is logging in with Email and Password and not with google.但目前它正在使用 Email 和密码而不是谷歌登录。

I am trying to fetch the name of the user so that is why I want to login with google.我正在尝试获取用户的名称,这就是我想使用 google 登录的原因。

This is a React part I did with Google Authentication.这是我使用 Google Authentication 完成的 React 部分。 It's not tailored to your code but I thought it might help you get where you want to go!它不是为您的代码量身定制的,但我认为它可能会帮助您到达您想去的地方!

class GoogleAuth extends React.Component {
  componentDidMount() {
    window.gapi.load("client:auth2", () => {
      window.gapi.client
        .init({
          clientId:
            "65397550643-g040rl648ka1p4d0h0t58crsa5v8ni1a.apps.googleusercontent.com",
          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();
  };

  renderAuthButton = () => {
    if (this.props.isSignedIn === null) {
      return null;
    } else if (this.props.isSignedIn) {
      return (
        <button className="ui red google button" onClick={this.onSignOutClick}>
          <i className="google icon" />
          Sign Out
        </button>
      );
    } else {
      return (
        <button className="ui green google button" onClick={this.onSignInClick}>
          <i className="google icon" />
          Sign in with Google
        </button>
      );
    }
  };

  render() {
    return <div className="item">{this.renderAuthButton()}</div>;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应原生中使用 firebase 检测 google auth 登录用户是否为 NewUser - How to detect whether google auth login user is a NewUser or not using firebase in react native 可能未处理的 Promise 拒绝 (id:0) | 反应原生 firebase 谷歌登录 - Possible Unhandled Promise Rejection (id:0) | React native firebase google login 用谷歌反应 firebase 登录 - react firebase login with google 如何使用非弹出式登录页面连接到Facebook? - How can I connect to facebook using a non popup Login page? 如何在React Native上使用访存功能登录API并捕获登录错误? - How can I log in to an API and catch login errors using fetch on React Native? 如何在 React Native Firebase 中解决这个问题? - How can I fix this problem in React Native Firebase? 我如何在 firebase 中创建一个自定义数据库并使用本机反应 - How can i create a custom database in firebase with react native 使用 firebase 反应本机自动登录 - React Native Auto Login with firebase 如何在 Google Drive 的特定文件夹上使用 React Native 上传文档? - How can I upload Document using React Native on Google Drive's Specific Folder? 如何在不使用全局作用域或redux的情况下在本地操作全局变量? - How can I work with globals in react-native without using the global scope or redux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM