首页 > 解决方案 > Firebase google认证需要双击登录

问题描述

我尝试使用 google 身份验证(使用 React 挂钩)登录到 Firebase,我需要单击该按钮两次。我希望用户在第一次点击后进入主页。这里有什么问题?

googleLogin.js

import firebase from "firebase/app";
import { auth,app } from "../../../../config/firebase";

export const googleLogin = async (e, information, setInformation, signup, login, history) => {
  var provider = new firebase.auth.GoogleAuthProvider();

  firebase
    .auth()
    .signInWithPopup(provider)
    .then( resp => {
      let {user, credential,additionalUserInfo: userInfo} = resp;
      let token = credential.accessToken;
      if (userInfo.isNewUser) signupWithGoogle(user, credential, userInfo);

    })
    .then(()=>{
      history.push('/')
    })
    .catch((error) => {
     ...
    });
};

const signupWithGoogle = (user, credential, userInfo)=>{
  app.firestore().collection('users').doc(user.uid).set({
    firstName: userInfo.profile.given_name,
    lastName: userInfo.profile.family_name});
    const batch = app.firestore().batch();
    const initData = [
      { Applied: { positionIds: [], title: 'Applied' } },
      { Contract: { positionIds: [], title: 'Contract' } },
      { Denied: { positionIds: [], title: 'Denied' } },
      { InProgress: { positionIds: [], title: 'In Progress' } },
      { ReceivedTask: { positionIds: [], title: 'Received Task' } },
    ];
    initData.forEach((doc) => {
      const docRef = app
        .firestore()
        .collection('users')
        .doc( user.uid)
        .collection('columns')
        .doc(Object.keys(doc)[0]);
      batch.set(docRef, Object.values(doc)[0]);
    });
    return batch.commit();
}

标签: firebasefirebase-authenticationreact-hooks

解决方案


I had the same problem to login and the problem was in my HTLM code. I was using a form. To resolve it, I used a simpler code in HTLM.


推荐阅读