Google登录报错auth/redirect-cancelled-by-user,按Firebase文档配置仍未解决
排查Cordova AngularFire Google登录出现
auth/redirect-cancelled-by-user错误 我帮你排查几个最可能导致这个错误的点,结合你提供的代码和配置来看:
1. 错误的getRedirectResult调用时机
这是Cordova环境下Google登录的常见坑:你当前在signInWithRedirect的链式then里调用getRedirectResult,但当用户跳转到浏览器完成授权后,你的App会重启,原来的代码上下文已经被销毁,这个then里的逻辑根本不会执行。
正确的做法是把getRedirectResult的调用放到App初始化的钩子中,比如在app.component.ts的ngOnInit里,这样每次App启动(包括授权后跳转回来)都会检查登录结果:
修改后的代码示例
// 登录组件中的signInWithGoogle方法 signInWithGoogle(): void { this.showLoading(); if (this.platform.is('android')) { console.log("android"); const provider = new firebase.auth.GoogleAuthProvider(); // 添加必要的权限 scope provider.addScope('email'); provider.addScope('profile'); this.afAuth.auth.signInWithRedirect(provider) .catch((error: any) => { this.hideLoading(); console.log("android login error: ", error); }); } if (this.platform.is('core')) { console.log("in core"); const provider = new firebase.auth.GoogleAuthProvider(); this.afAuth.auth.signInWithPopup(provider) .then((result: any) => { if (result) { this.handleUser(result); } }) .catch((error: any) => { this.hideLoading(); console.log("error: ", error); }); } } // 在app.component.ts的ngOnInit中检查跳转结果 ngOnInit() { if (this.platform.is('android')) { firebase.auth().getRedirectResult() .then((result: any) => { if (result && result.user) { this.hideLoading(); this.handleUser(result); } }) .catch((error: any) => { this.hideLoading(); console.log("Redirect result error: ", error); }); } }
2. 验证Universal Links是否正常工作
auth/redirect-cancelled-by-user也可能是因为授权后无法正确跳转回App,导致Firebase认为用户取消了操作。你可以通过以下步骤验证:
- 测试链接跳转:用adb命令测试你的回调链接是否能打开App:
如果App没有被唤起,说明Universal Links配置有问题,需要检查:adb shell am start -W -a android.intent.action.VIEW -d "https://test1-2366d.firebaseapp.com/__/auth/callback"- 确认Firebase控制台中添加的SHA-256指纹和你当前测试用的App签名一致(debug和release签名要分别添加)
- 访问
https://test1-2366d.firebaseapp.com/.well-known/assetlinks.json,确认文件中包含你的App包名和对应的SHA指纹 - 确保
cordova-plugin-universal-links插件安装正确,且版本兼容你的Cordova Android版本 - 确认
config.xml中的<preference name="AndroidLaunchMode" value="singleTask" />已经正确配置,这个是保证跳转回同一App实例的关键
3. 检查Google登录的配置细节
- 在Firebase控制台的Auth > 登录方法 > Google中,确认你的动态链接域名
bhb7m.app.goo.gl和Firebase App域名test1-2366d.firebaseapp.com都已添加到授权域列表中 - 可以给GoogleAuthProvider添加自定义参数,强制用户选择账号,避免授权中断:
provider.setCustomParameters({ prompt: 'select_account' });
4. 插件版本兼容性检查
确保你安装的所有相关插件版本兼容:
- 确认
angularfire2、firebaseSDK的版本和Cordova插件版本匹配 - 检查
cordova-plugin-firebase-auth(或@ionic-native/firebase-auth)是否为最新稳定版
先从调整getRedirectResult的调用时机开始测试,这是最可能解决你问题的点。如果还是不行,再逐一验证Universal Links的配置。
内容的提问来源于stack exchange,提问作者Noman
相关产品推荐
相关产品推荐

