firebase - 使用 Firebase signInWithRedirect 是否可以将其配置为将我重定向回特定页面

标签 firebase angularfire2

当我打电话
this.afAuth.auth.signInWithRedirect(provider) .then((credential) => { }) .catch((error) => this.handleError(error));
重定向工作正常,并由 firebase 处理,将我返回到登录页面。然而, promise 需要一段时间才能完成
this.afAuth.auth.getRedirectResult().then((result) => { //Login or move to request additional info });
由于页面已经完全刷新,我没有任何信息让我知道页面不应该显示登录按钮,而是应该显示加载状态。

有没有人找到解决这个问题并保持加载状态的好方法,我最初的尝试是使用角度路由来传递加载状态,但令人讨厌的是 signInWithRedirect 服务似乎没有更改它重定向到的 url。

最佳答案

FirebaseUI使用本地存储来维护是否显示加载状态( source )。你也应该能够做到这一点。但是,如果您不想处理本地存储并且不介意污染 URL,您可以在调用 signInWithRedirect 之前向 URL 添加唯一的查询/哈希参数。 (Firebase Auth 序列化当前 URL 并将其作为 redirect_uri 传递给 OAuth 端点 ( source )。

使用哈希参数的示例

// you can use any string here; just make sure to use the same one after reload
window.location.hash = "redirecting"
this.afAuth.auth.signInWithRedirect(provider)
  .then((credential) => {
  })
  .catch((error) => this.handleError(error));

然后你可以在你的组件渲染后立即检查(重定向后):
this.loading = (window.location.hash === "redirecting")
// Clear the hash
window.location.hash = ""

希望这可以帮助。

编辑

您还可以使用 window.history.replaceState 实现将重定向 URL 设置为您想要的任何内容。和 window.history.pushState在重定向之前更改 URL(类似于上面的哈希示例)。

关于firebase - 使用 Firebase signInWithRedirect 是否可以将其配置为将我重定向回特定页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593590/

相关文章:

firebase - 我可以在没有服务器端代码的情况下以安全的方式与 firebase 集成,而无需进行用户身份验证吗?

android - 我不能在不将其链接到 BigQuery 的情况下跟踪 firebase 上的简单按钮单击事件吗?

javascript - Angular AngularFire2 简单连接

javascript - AngularFire 2 - FirebaseApp 与 FirebaseRef

typescript - Firebase Cloud Functions Firestore 触发器产生 : Error: 7 PERMISSION_DENIED: Missing or insufficient permissions

firebase - 查询 Firebase 年/月/日嵌套中的特定 ID

javascript - Angular 4 异步数据存储

typescript - 当用户登录(auth 不为 null )时,如何在 AngularFire2 中获取对象?

android - Ionic 3 Firebase Google Auth 在 Android 设备上停止工作

android - install_referrer Intent 弃用和更新库