簡體   English   中英

如何將 Firebase 用戶 ID 導出到另一個 JS 文件?

[英]How To Export Firebase User ID To Another JS File?

當談到 NodeJS 和 Firebase 通過 NodeJS 時,我是初學者。 我正在嘗試導出當前登錄到我的網站的用戶的用戶 ID,並將該用戶 ID 導出到另一個 JavaScript 文件以供在那里使用。

我使用了以下代碼:

import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
} else {
    // User is signed out
    // ...
  }
});

來自Firebase 的身份驗證文檔頁面(通過 Github) 我已將此代碼用作簡單測試的基礎,但我意識到我放在這里並導出的任何代碼都返回未定義。

這是我的代碼的設置方式:
firebase.js 文件:

const { initializeApp } = require('firebase/app');
const { getFirestore, collection, getDocs } = require('firebase/firestore/lite');
const { getAuth, onAuthStateChanged } = require("firebase/auth");

const firebaseConfig = {
    apiKey: "xxxxxxxxxxxxxxx_xxxxxxxxx-xxxxxxxxxxxx",
    authDomain: "test-database-346d9.firebaseapp.com",
    projectId: "test-database-336c8",
    storageBucket: "test-database-346d9.appspot.com",
    messagingSenderId: "59s8107465422",
    appId: "X:xxxxxxxxxxxx:web:xxxxxxxxxxxxxxxxxxxxxx"
};

const app = initializeApp(firebaseConfig);
const db = getFirestore();

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
    if (user) {
        const uid = user.uid;
        module.exports = {uid};
    } 
    else {
        return;
    }
});

server.js 文件:

const user_id = require('./firebase');
console.log(user_id.uid);

我已經用其他代碼片段對此進行了測試,它們工作得很好。 我只是不明白為什么這似乎無法解決。

我嘗試的另一個嘗試是:
firebase.js 文件:

async function call() {
    const querySnapshot = await getDocs(collection(db, "user-account"));
    querySnapshot.forEach((doc) => {
        const auth = getAuth();
        onAuthStateChanged(auth, (user) => {
            if(user) {
                if(user.email !== doc.data().email) {
                    return;
                }
                else {
                  let uid = user.uid;
                  module.exports = {uid};
                }
            }
        });
    });
}
call();

這似乎也行不通。 上面的所有代碼都在檢查用戶 email 是否與 Firestore 數據庫中的 email 匹配。 但是,我不明白為什么我不斷收到未定義的返回。 有人可以告訴我正確的方法來實現我想要做的事情嗎? 謝謝你。

I was having same problem when I started working with firebase + NodeJs, the google's documentation seems to be messy for me so I did a little of research and found a custom react hook for firebase which simplify integrating firebase on my nodeJs app very easily. 這個自定義反應掛鈎被稱為:react-firebase-hooks 您可以在此處找到更多信息: https://www.npmjs.com/package/react-firebase-hooks基本上在您的情況下,您需要的代碼是這樣的:

 //imports import { useAuthState } from 'react-firebase-hooks/auth'; import { getAuth } from 'firebase/auth'; const auth = getAuth(firebaseApp); const Page = () =>{ //react-firebase hook const [user, loading, error] = useAuthState(auth, options); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

就是這樣,用戶變量是 object,它將包含您要查找的所有信息(用戶 ID,email ...)

暫無
暫無

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

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