Firebase Google登录异常:无需密码且跳转错误页面
Firebase Google登录异常排查与解决
问题现象
- Google登录无需输入密码直接跳转错误页面,但邮箱登录功能正常
- 已确认JS配置、Firebase控制台、Google Cloud API密钥中均配置了正确的授权重定向URI
- 点击Google登录按钮后,页面跳转至类似地址:
www.myurl.com/__/auth/handler?apiKey=xxxxxxxxxxxxxxxxxxxxx&appName=%5BDEFAULT%5D-firebaseui-temp&authType=signInViaRedirect&redirectUrl=https%3A%2F%2Fwww.myurl.com%2Fsign-in&v=9.13.0&providerId=google.com&scopes=profile
网站现有代码
头部引入的脚本与样式
<script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-app-compat.js"></script> <script src="https://www.gstatic.com/firebasejs/9.13.0/firebase-auth-compat.js"></script> <script src="https://www.gstatic.com/firebasejs/ui/6.0.2/firebase-ui-auth.js"></script> <link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/6.0.2/firebase-ui-auth.css" />
页面主体代码
<div id="firebaseui-auth-container"></div> <script type="text/javascript"> // FirebaseUI config. var uiConfig = { // Important: the following URL should be in both Firebase and Gloogle Cloud authorised clients signInSuccessUrl: "/private/main", // autoUpgradeAnonymousUsers: true, signInOptions: [ // Leave the lines as is for the providers you want to offer your users. firebase.auth.GoogleAuthProvider.PROVIDER_ID, firebase.auth.EmailAuthProvider.PROVIDER_ID, ], // tosUrl and privacyPolicyUrl accept either url string or a callback. // Terms of service url/callback. tosUrl: "/terms-of-services", // Privacy policy url/callback. privacyPolicyUrl: "/privacy-policy" }; // App's Firebase configuration const firebaseConfig = { apiKey: "xxxxxxxxxxxxxxxxxx", authDomain: "myurl.com", projectId: "xxxxxxxxxxxxxxxxx", storageBucket: "xxxxxxxxxxxxxxxx", messagingSenderId: "xxxxxxxxxxxxxxxxx", appId: "xxxxxxxxxxxxxxxxx", }; // Initialize Firebase const app = firebase.initializeApp(firebaseConfig); // Initialize the FirebaseUI Widget using Firebase. var ui = new firebaseui.auth.AuthUI(firebase.auth()); // The start method will wait until the DOM is loaded. ui.start("#firebaseui-auth-container", uiConfig); </script>
排查与解决建议
修正AuthDomain配置
当前authDomain设为myurl.com,需确保与Firebase控制台中认证模块的授权域名完全一致(含www前缀及https协议)。跳转URL中的redirectUrl是https://www.myurl.com/sign-in,必须完全匹配Firebase和Google Cloud中配置的重定向URI,大小写、前缀、协议都不能出错。对齐库版本兼容性
Firebase核心库使用9.13.0,FirebaseUI为6.0.2,版本不匹配可能引发异常。升级FirebaseUI到与Firebase 9.x适配的版本,保持两者版本系列一致。校验Google Cloud OAuth配置
进入Google Cloud控制台的OAuth 2.0客户端ID页面:- 确认已添加
https://www.myurl.com/__/auth/handler和https://www.myurl.com/sign-in作为授权重定向URI - 客户端ID需与Firebase项目绑定的Web应用对应
- 排查是否存在URI拼写错误(比如遗漏
https、多余空格)
- 确认已添加
清理浏览器缓存
旧的OAuth会话缓存可能导致跳转异常,清除浏览器的Cookie和缓存后重新测试。补充重定向回调处理
在页面初始化代码中添加重定向结果处理逻辑,确保捕获登录状态:firebase.auth().getRedirectResult().then(result => { if (result.error) console.error("登录失败:", result.error); }).catch(err => console.error("回调处理异常:", err));
内容的提问来源于stack exchange,提问作者Escachator
相关产品推荐
相关产品推荐

