当我打电话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/