簡體   English   中英

|已解決| 從 Firebase 獲取用戶身份驗證數據並將其添加到 Firebase DB

[英]|SOLVED| Get User Auth data from Firebase & add it to Firebase DB

你好!

編輯~我最終設法做到了這一點,並想分享它,以防其他人需要它。 我發現的大多數教程都已經過時了,而且它們似乎都不適合我。 但我終於讓一切正常工作,所以它來了!


注冊 - (我創建了一個注冊表單,其中包含用戶名、額外信息、密碼和電子郵件的輸入字段)

確保導入所有要使用的 firebase 腳本,並且在所有這些腳本之上,即 firebase 應用程序主腳本。 在我的情況下,我只需要身份驗證和數據庫 - 在所有這些之下,您將您的 Firebase 應用配置放入一個外部 .js 文件中,您將在其中使用 firebase 函數,或者將其全部寫在那里。 這是我自己犯的一個非常愚蠢的錯誤,我一直在控制台上出錯。 這是因為我一直在嘗試在導入 firebase 主腳本之前調用我的外部 .js 文件,這沒有意義,對嗎?

所以這是我用於注冊功能的.js文件:

//On a different .js file where I make use of most of my functions I've added this part 
//(just because I've defined more const for all my functions and I wanted to have them all 
//in one place):

//getting all elements -- I've only put in this example the ones that I've used for Sign Up
const signupBtn = document.getElementById("btnsignUp");

const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const userId = document.getElementById('txtName');
const discord = document.getElementById('txtDiscord');
const bday = document.getElementById('txtBday');
const gender = document.getElementById('txtGender');
const imgURL = document.getElementById('txtimgURL');

//getting references to the apps
const auth = firebase.auth();
const database = firebase.database();
const rootRef = database.ref('users');


//------------------------------------------------//


//firebase SIGN UP.js
signupBtn.addEventListener("click", function(){

    var email = txtEmail.value;
    var pass = txtPassword.value;

    //Signing up
    auth.createUserWithEmailAndPassword(email, pass)
    .then(() => {

      //send verification email
      sendVerificationEmail();
    }) 
    .catch(function(error) {
    // Handle Errors here.
    //var errorCode = error.code;
    var errorMessage = error.message;
    alert("Error :" + errorMessage);
    });

  });
  //verification email function
  var sendVerificationEmail = () => {
    auth.currentUser.sendEmailVerification()
    .then(() => {
      alert("Verification Email Sent! Check your mailbox.")
    })
    .catch(error => {
      alert("Error :" + errorMessage);
    })
  }      

  //DATABASE
  //'set' adds new data to he db
  signupBtn.addEventListener('click', (e) => {
    e.preventDefault();
    rootRef.child(userId.value).set({
        Email: txtEmail.value,
        Discord: discord.value,
        Gender: gender.value,
        Birthday: bday.value,
        ImgURL: imgURL.value,
        CC: 0,//Here I've added some more info that's be stored too along with 
        RS: 0,//the data that the user has provided
        Rupreets: 0,
        Bag: 1,//1: small, 2: medium, 3: big
    })    
  }); 
//And that's all!

就我而言,我對數據庫部分所做的事情是這樣的:

-App name-
|
+--Users:
 |
 +--username1
  |
  +-info1
  |
  +-info2
  |
  +-info2
 |
 +--username2
  |
  +-info1
  |
  +-info2
  |
  +-info2

嗯,我希望這也能幫助其他人nn

歡迎使用堆棧溢出!

幾件事:

  1. 您實際上writeUserData在代碼片段中調用writeUserData 你只是定義它。 如果您不調用它,則不會寫入任何內容。

  2. userId從未定義,因此即使您調用writeUserData ,您的數據庫路徑也將是undefined 您需要從firebase.auth().currentUser.uid獲取 userId 。 有關更多信息,請參閱此 Firebase 文檔: https ://firebase.google.com/docs/auth/unity/manage-users#get_a_users_profile 。

- 編輯 -

這是一個代碼示例。 我沒有把所有的東西都放進去,只是相關的遺漏:

//Signing up
    firebase.auth().createUserWithEmailAndPassword(email, pass)
    .then((data) => {

// createUserWithEmailAndPassWord returns a promise, which, when resolved will contain various user-related properties, so 
let id = data.User.uid;

      function writeUserData(userId, name, email, imageURL) {
        firebase.database().ref('Users/' + userId).set({
          Username: name,
          Email: email,
          Profile_pic: imageURL,
        });
      }

// call your function, referencing the id above
writeUserData(id, name, email, imageURL)

如果承諾和調用函數的想法不舒服,您可以查看 Javascript 學習資源,如javascript.info

祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM