You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Auth调用getRedirectResult时捕获到'undefined'错误的问题求助

Firebase Auth调用getRedirectResult时捕获到'undefined'错误的问题求助

各位大佬好,我现在遇到一个Firebase Auth的问题,想请教下大家。我写了一个auth.js文件来处理用户的注册/登录,支持邮箱、Google和Facebook三种方式。目前卡在Google登录后的重定向结果处理上——用户通过重定向完成Google登录后,我原本期望代码能把用户数据写入Firestore,然后跳转到其他页面,但实际情况是控制台输出了undefined错误。

下面是我的代码:

function signInWithGoogle(){
  signInWithRedirect(auth, googleProvider);
}

// Get redirect result
getRedirectResult(auth)
.then(async (result) => {
  const credential = GoogleAuthProvider.credentialFromResult(result);
  const token = credential.accessToken;
  const user = result.user;

  const q = query(collection(firestore, "users"), where("uid", "==", user.uid));
  // Make a snapshot of the current user data
  const querySnapshot = await getDocs(q);

  // If the doc is empty, write new data. Else, update it
  if (querySnapshot.empty){
    const docRef = await addDoc(collection(firestore, "users"), {
      uid: user.uid,
      role: "Student",
      last_login: dt,
      fname: null,
      lname: null
    });
    // Go to profile.html
    goToUrl("profile.html");
  } else {
    // If there is already a document, update the last login property
    querySnapshot.forEach(async (document) => {
      const docReference = doc(firestore, "users", document.id);
      await updateDoc(docReference, {
        last_login: dt
      });
    });
    // Go to profile.html
    goToUrl("profile.html");
  }
}).catch((error) => {
  console.log(error.code);
});

我排查了一下,怀疑是不是重定向后result对象为空?或者是credential获取失败?但控制台只输出undefined,根本看不到具体的错误码,这让我有点无从下手。有没有大佬遇到过类似的问题,或者能帮我分析下可能的原因?

备注:内容来源于stack exchange,提问作者NATHAN TOLLEY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 16:03:01