angularfire 中 $getAuth 和 $onAuth 的区别

Difference between $getAuth and $onAuth in angularfire

我正在阅读 angularfire 文档,我发现 firebase 提供了两个类似的服务 $getAuth$onAuth,它们都是为检查用户身份验证而编写的。但是我找不到两者之间的任何区别,有人可以为我描述一下吗?

而且我还想知道使用这些服务的最佳方案是什么。谢谢

link of official documentation

正如 $getAuth() 的文档所说:

Synchronously retrieves the current authentication state of the client

而对于 $onAuth() 它说:

Listens for changes to the client’s authentication state. The provided callback will fire when the client’s authenticate state changes.

因此,如果您需要知道用户是否已通过身份验证,您可以在代码中调用 $getAth()。假设您希望在用户通过身份验证时在计算中使用不同的价格:

var productPrice = 50;
$scope.price = auth.$getAuth() ? 0.8 * productPrice : productPrice;

另一方面,如果您想在用户通过身份验证时做一些事情,您可以使用$onAuth()。一个典型的例子是当用户 sign-in/get 退出时将用户路由到另一个 URL。