-1

我有一个聚合物入门套件应用程序,其中包含多个子路线以及与这些路线相关的各种类型的记录。示例 URL 可能是//myapp/records/{recordId}/subRoute. 我希望应用程序状态在重新加载时保持不变。我试过这样:

<app-route
    route="{{route}}"
    pattern="/records/:recordId"
    data="{{routeData}}"
    tail="{{subroute}}"
></app-route>
<app-route
    route="{{subroute}}"
    pattern="/:page"
    data="{{subRouteData}}"
></app-route>

<firebase-document
    id="recordsDocument"
    path="/records/[[routeData.recordId]]"
    data="{{record}}"
></firebase-document>

...

<script>
  routePageChanged(page) { // takes subRouteData.page
    if (!user) {
      set('page', 'login'),
    } else if (!routeData.recordId) {
      set('page', 'start')
    } else {
      set('page', page)
    }
  }
</script>

但在这些情况下,浏览器总是会出现在登录页面上,因为在 firebase 响应用户之前调用了 routePageChanged。user当该对象在firebase响应之前不存在时,如何让我的应用程序从依赖于auth'd firebase存在的地址栏路由正确加载?

4

1 回答 1

0

你可以做几件事。由于您正在等待用户,因此您需要初始化用户对象。

在 firebase-auth 中,有一个用户对象:

 user: {
   type: Object,
   readOnly: true,
   value: null,
   notify: true
 },

您可以侦听该对象的更改。

另一种方法是像这样处理它:

this.$.auth.signInWithPopup()
.then(function(response) {// successful authentication response here})
.catch(function(error) {// unsuccessful authentication response here});

上述代码成功后,应设置用户对象,您可以使用它检查您的初始路由。

我想不出任何其他选择。

除此之外,我必须推荐这些文档,当然:P

https://github.com/firebase/polymerfire/blob/master/firebase-auth.html

于 2016-12-05T17:59:31.690 回答