聚合物1. *和Firebase
如何在this.$$('#ironAjax').generateRequest();
之后调用return '{"Authorization": "Bearer ' + token + '"}';
时执行异步操作?我必须返回标头值,然后调用iron-ajax请求。
<iron-ajax
url="[[HOST]][[LISTINGS]]?foo=foo"
id="ironAjax"
handle-as="json"
headers="[[setAuth()]]",
on-error="errorHandler"
loading="{{ironLoading}}"
last-response="{{listings}}"></iron-ajax>
pageActivated: function(activated) {
if (activated) {
this.setAuth();
}
},
setAuth: ()=> {
firebase.auth()
.onAuthStateChanged((token) => {
return '{"Authorization": "Bearer ' + token + '"}';
this.$$('#ironAjax').generateRequest(); // NEED TO CALL AFTER
// HEADER IS SET
})
答案 0 :(得分:0)
我能够通过直接修改标题来处理这种情况。希望这可以帮助处于同样情况的其他人:
行为:
<script>
AuthIronAjax = {
rawSetAuth: (user, ajax) => {
if (user) {
user.getIdToken().then((token) => {
ajax.headers['Authorization'] = 'Bearer ' + token;
ajax.generateRequest();
});
}
},
setAuth: function (ajax) {
firebase.auth()
.onAuthStateChanged((user)=> this.rawSetAuth(user, ajax));
}
};
</script>
使用它的元素。注意:铁页面与隐含的主机属性绑定activated
一起使用:
observers: ['pageActivated(activated)'],
behaviors: [AuthIronAjax],
pageActivated: function(activated) {
if (activated) {
this.setAuth(this.$.ironAjax);
}
},