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

Ionic/Angular中Firebase Google授权登录后页面跳转不生效如何解决

Firebase Google 重定向登录后路由跳转失效解决方案

问题根因

signInWithRedirect 方法触发后会直接将当前页面重定向到Google身份验证页面,原页面的执行上下文会被完全销毁,因此await之后的代码永远不会被执行。你需要在用户验证完成跳转回应用的入口处监听登录结果,再执行跳转逻辑。

修复步骤

  • 调整触发登录的逻辑,删除原有写在await signInWithRedirect后的跳转代码
async signInWithGoogle() {
  // 仅保留触发重定向登录的逻辑
  await this.afAuth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
}
  • 在应用根组件或登录页的ngOnInit生命周期钩子中监听重定向登录结果
ngOnInit() {
  // 监听重定向登录返回的验证结果
  this.afAuth.getRedirectResult()
    .then((result) => {
      // 验证存在登录用户时执行跳转
      if (result.user) {
        this.ngZone.run(() => {
          this.router.navigate(['tabs/home']);
        });
      }
    })
    .catch((error) => {
      // 可自行扩展登录异常处理逻辑
      console.error("登录异常:", error);
    });
}

内容的提问来源于stack exchange,提问作者Milchgraf

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 02:57:02