【问题标题】:Dom-if by user role with polymer & firebase using firebase-auth and google as providerDom-if 按用户角色与聚合物和 firebase 使用 firebase-auth 和 google 作为提供者
【发布时间】:2017-06-15 14:38:18
【问题描述】:

我试图仅向某些用户显示 dom-if 模板,但我不知道该怎么做。这是我正在尝试做的伪代码:

 <template is="dom-if" if="{{user.isAdmin}}" restamp="true">
        <hr />
        <h1>Add new conference:</h1>
        <paper-input
        id="title"
        label="Título"
        type="text"
        name="title">
    </paper-input>
    <paper-fab id="saveFab" icon="cloud-upload" on-tap="add"></paper-fab>
</template>
<script>
        Polymer({
            is: 'my-conferences',
            properties: {
                data: Array,
                user: Object,
            },

...

其中“用户”是我从&lt;firebase-auth&gt; 元素中获得的对象。像 user.isAdmin 这样的东西是我想要实现的。

有什么想法吗?谢谢!

【问题讨论】:

  • 您能发布一个完整的代码示例吗?目前,对于您的 my-conferences 元素,我没有看到任何 dom-module。如果你没有它,那么这就是你的问题。如果您确实有它,那么问题出在其他地方,我们需要查看更多代码才能对其进行分析。
  • 我有默认的聚合物元素结构(包括 dom-module)。这里所有的源代码:github.com/zolastro/HackersWeekMalaga/blob/master/src/my-app/…
  • 您如何验证您的管理员身份?您对 firebase 使用哪种身份验证类型?也许看看这里:stackoverflow.com/questions/17607101/…
  • 我使用“google”作为帐户提供商。这个想法是允许这些谷歌帐户之一成为管理员。不过,我会尝试使用firebaseRef.authWithCustomToken
  • 您是否有幸创建了管理员帐户?

标签: authentication firebase polymer firebase-authentication dom-if


【解决方案1】:

这里有一种方法可以做到这一点。

  1. 在您的 Firebase 上创建一个具有相同 ID 的用户记录。

然后:

    <firebase-auth id="auth" provider="google" user="{{auth}}" on-error="showError">
</firebase-auth>

<app-user
    uid="[[auth.uid]]"
    user="{{user}}"></app-user>

应用用户元素是

<script>
    (function() {
        'use strict';
        Polymer({
            is: 'app-user',

            properties: {
                uid: {
                    type: String,
                    reflectToAttribute: true,
                    notify: true,
                    observer: '_uidChanged'
                },
                user: {
                    type: Object,
                    value: null,
                    notify: true
                }
            },

            _uidChanged: function (uid) {

                if (this.uid) {
                    firebase.database().ref('/users/' + uid).once('value', function(snapshot) {
                        if (snapshot.val()) {
                            this.user = snapshot.val();
                        }
                    }.bind(this));

                }
            }
        });
    })();
</script>

如果您需要更多帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 2021-07-13
    • 2022-01-21
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    相关资源
    最近更新 更多