在angularfire中$ getAuth和$ onAuth之间的区别 [英] Difference between $getAuth and $onAuth in angularfire
问题描述
angularfire
docs,发现了两个类似的服务 $ getAuth
和 $ onAuth
由 firebase
给出,两者都是为了检查用户认证而编写的。但我找不到他们之间的任何区别,任何人都可以为我描述他们?我也想知道什么是每个使用的好方案这些服务。谢谢
官方文档链接 $ getAuth()说:
$ b
同步检索客户端的当前身份验证状态
而 $ onAuth()
则表示:
侦听客户端身份验证状态的更改。所提供的回调将在客户端的身份验证状态改变时触发。
所以你可以调用 $ getAth()
在你的代码中,如果你需要知道用户是否被认证。假设您想在计算时使用不同的价格进行验证:
var productPrice = 50;
$ scope.price = auth。$ getAuth()? 0.8 * productPrice:productPrice;
另一方面,如果您希望在 >用户通过身份验证,您可以使用 $ onAuth()
。一个典型的例子是当用户登录/退出时将用户路由到不同的URL。
I was reading angularfire
docs, and i found two similar services $getAuth
and $onAuth
given by firebase
and both are written for checking user authentication. But i can't found any differences between both of them, can anybody describe them for me ?
and i also want to know that what is the good scenario to use each of these services. Thanks
link of official documentation
As the documentation for $getAuth()
says:
Synchronously retrieves the current authentication state of the client
While for $onAuth()
it says:
Listens for changes to the client’s authentication state. The provided callback will fire when the client’s authenticate state changes.
So you'd call $getAth()
in your code if you need to know whether the user is authenticated. Say that you want to use a different price in a calculation when the user is authenticated:
var productPrice = 50;
$scope.price = auth.$getAuth() ? 0.8 * productPrice : productPrice;
If on the other hand you'd want to do something based on when the user is authenticated, you'd use $onAuth()
. A typical example would be to route the user to a different URL when they sign-in/get signed out.
这篇关于在angularfire中$ getAuth和$ onAuth之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!