如何将Javascript Promise链接与Firebase身份验证和Firebase一起使用? [英] How do I use Javascript Promise Chaining with Firebase Authentication and Firebase?

查看:49
本文介绍了如何将Javascript Promise链接与Firebase身份验证和Firebase一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用用户的Firebase用户ID使用存储在Firebase中的密钥.

I need to utilize a key stored in my firebase using the user's firebase user id.

执行我的功能的步骤如下:

The steps to my function are the following:

1)认证后获取Firebase用户ID

1) Get the Firebase User ID after authentication

2)使用Firebase用户ID,拉出存储的API密钥值(我将其保存在节点中:app/{Firebase用户ID})

2) Using the Firebase User ID, pull the stored API Key value (which I saved in a node: app/{Firebase User Id})

3)使用存储的API密钥值,运行我的最后一个函数

3) Using the stored API Key value, run my last function

经过研究,我得出的结论是,我应该对下面的代码使用Javascript Promise Chaining,这对我来说是很困难的

After doing some research, I've come to the conclusion that I should use Javascript Promise Chaining to the below code, which I'm having a difficult time doing

firebase.initializeApp({databaseURL: "{}"});
var dbRef = firebase.database();

function pull_api_key_from_firebase_after_auth(func){
  firebase.auth().onAuthStateChanged((user) => {
  if (user) {
     var app_ref = dbRef.ref('app').child(user.uid);
     app_ref.on("child_added", function(snap) {
        dictionary_object = snap.val()
        api_key = dictionary_object.api_key
        func(api_key)
      })
    }
  });
}

function final_function(api_key){
   console.log('processing final function')
}

pull_api_key_from_firebase_after_auth(final_function)

或者,我想将api_key设为这样的全局变量:

Alternatively, I'd like to make api_key a global variable as such:

function pull_api_key_from_firebase_after_auth(func){
      firebase.auth().onAuthStateChanged((user) => {
      if (user) {
         var app_ref = dbRef.ref('app').child(user.uid);
         app_ref.on("child_added", function(snap) {
            dictionary_object = snap.val()
            api_key = dictionary_object.api_key
            localStorage.setItem('api_key',api_key)

          })
        }
      });
    }

api_key = localStorage.getItem('api_key')
final_function(api_key)

但是我无法弄清楚如何使final_function以及其他函数等到定义api_key为止

However I cant figure out how to make final_function as well as my other functions wait until api_key is defined

推荐答案

必须使用 firebase.auth().onAuthStateChanged 来获取uid bcs .auth.currentUser.uid 在页面加载时将不可用.它显示了一个微秒后(它是异步的).

You must use firebase.auth().onAuthStateChanged to get the uid bcs .auth.currentUser.uid will NOT be available on page load. It shows up a microsecond after (it's asynchronous).

要做您想做的事情,只需构建一个promise函数并在 .onAuthStateChanged .then 中调用它,然后执行您的其他功能.您自定义的诺言可能如下所示:

To do what you want simply build out a promise function and call it within .onAuthStateChanged and .then do your other function. You custom promise might look like:

function fetchUserData(){
    return new Promise(function(resolve, reject){
        var db = firebase.firestore(); // OR realtime db
        db.collection("users").get().then(function(snap) {
            resolve (snap);
        }).catch(function(error) {
            reject (error);
        });
    }); 
}

.onAuthStateChange 内执行以下操作:

fetchUserData().then((snap) => {
    // do your thing

})
.catch((error) => {
    console.log(error);
});

这篇关于如何将Javascript Promise链接与Firebase身份验证和Firebase一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆