简体   繁体   English

firebase web 注册与 google 仅在第二次点击后才有效?

[英]firebase web sign up with google only works after clicking the second time?

I have got a react create app configured with google firebase.我有一个配置了 google firebase 的 react create 应用程序。 I am accepting both signup with email and signup with google methods.我接受电子邮件注册和谷歌方法注册。

the regular signup with email works fine.使用电子邮件的常规注册工作正常。 but signInWithGoogle() method only works if user clicks on the google icon 2 times.signInWithGoogle()方法仅在用户点击谷歌图标 2 次时才有效。 I am using history.push to redirect user to /dashboard if the signInWithGoogle() promise resolves successfully.如果signInWithGoogle()承诺成功解析,我将使用history.push将用户重定向到/dashboard

Current Behavior: when I click on the google button which fires this function, I get popup window:当前行为:当我点击触发此功能的谷歌按钮时,我得到弹出窗口: 注册弹出窗口

but after successful login in this window exits, but the site stays on the same signup form.但是在此窗口中成功登录后退出,但该站点保留在相同的注册表单上。 However, if I repeat the action, (click the google icon, and signing with google id again (twice), then it redirects to dashboard.但是,如果我重复该操作(单击 google 图标,然后再次使用 google id 签名(两次),则它会重定向到仪表板。

Expected Behavior:预期行为:

It should redirect to /dashboard without needing to repeat the action twice.它应该重定向到/dashboard而不需要重复操作两次。

CODE: components/SignUpSide.js代码:组件/SignUpSide.js

import React from "react";
import { useState, useContext, useEffect } from "react";
import { Link, useHistory } from "react-router-dom";
import FirebaseContext from "../context/firebase";



export default function SignUpSide() {

const history = useHistory();
const { firebase, googleProvider } = useContext(FirebaseContext);

const auth = firebase.auth();

  async function signInWithGoogle() {
    auth
      .signInWithPopup(googleProvider)
      .then((res) => {
        const newUser = res.user;

        const checkDomains = ["gmail.com", "yahoo.com", "outlook.com"];
        const domain = newUser.email.substring(
          newUser.email.lastIndexOf("@") + 1
        );
        let accountType = "";
        if (checkDomains.includes(domain)) {
          accountType = "limited";
        } else {
          accountType = "Standard";
        }

        firebase
          .firestore()
          .collection("users")
          .add({
            userId: newUser.uid,
            username: newUser.displayName.toLowerCase(),
            // fullName,
            emailAddress: newUser.email.toLowerCase(),
            following: ["2"],
            followers: [],
            dateCreated: Date.now(),
            firstLogin: true,
            accountType: accountType,
          });

        history.push("/dashboard");
      })
      .catch((error) => {
        alert.error(error.message);
      });
  }

return (
<>
  <Button onClick={signInWithGoogle}>
                    <img
                      className={classes.icons}
                      src="https://img.icons8.com/fluent/100/000000/google-logo.png"
                      alt="google logo"
                    />
                  </Button>
</>
 )
}

OK so I figured out the answer sharing it here for the future.好的,所以我想出了将来在这里分享的答案。

The problem: during my debugging, I found out that when the Google popup window gets removed, it actually refreshes the page it originated from which destroys the context and the entire component's code get to run again because of the refresh.问题:在我的调试过程中,我发现当 Google 弹出窗口被删除时,它实际上刷新了它源自的页面,从而破坏了上下文,并且整个组件的代码由于刷新而再次运行。 since my 'redirect' code was at the bottom, on the first time, the execution isn't reached to the redirect code so it never works.由于我的“重定向”代码位于底部,因此第一次执行未到达重定向代码,因此它永远不会起作用。 however, during the second time around, the Google popup code never runs and it reaches the redirect code so the page redirects.然而,在第二次期间,Google 弹出代码永远不会运行,它到达重定向代码,因此页面重定向。

the solution: I wrote an if statement in login page's useEffect, saying something like:解决方案:我在登录页面的 useEffect 中写了一个 if 语句,内容如下:

if (user) {
 //redirect to dashboard
}

so as soon as a user is gotten thanks to popup exit, the app gets redirected to dashboard.因此,一旦通过弹出式退出获得用户,应用程序就会被重定向到仪表板。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM