简体   繁体   English

如何将 Firebase 用户 ID 导出到另一个 JS 文件?

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

I'm a beginner when it comes to NodeJS and Firebase through NodeJS.当谈到 NodeJS 和 Firebase 通过 NodeJS 时,我是初学者。 I'm trying to export the user ID of a user that's currently logged into my website, and export that user ID to another JavaScript file to be used there.我正在尝试导出当前登录到我的网站的用户的用户 ID,并将该用户 ID 导出到另一个 JavaScript 文件以供在那里使用。

I've used the following code:我使用了以下代码:

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
    // ...
  }
});

from Firebase's documentation page for authentication (via Github) .来自Firebase 的身份验证文档页面(通过 Github) I've used this code as a base to simply test out, but I've realized that any code I put in here and export, returns undefined.我已将此代码用作简单测试的基础,但我意识到我放在这里并导出的任何代码都返回未定义。

Here is how my code is set up:这是我的代码的设置方式:
firebase.js File: 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 File: server.js 文件:

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

I've tested this out with other code snippets and they work just fine.我已经用其他代码片段对此进行了测试,它们工作得很好。 I just don't understand why this can't seem to work out.我只是不明白为什么这似乎无法解决。

Another attempt I tried was this:我尝试的另一个尝试是:
firebase.js File: 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();

This also doesn't seem to work out.这似乎也行不通。 All the code above is doing is checking whether the user email matches the email inside the Firestore database as well.上面的所有代码都在检查用户 email 是否与 Firestore 数据库中的 email 匹配。 I don't understand why I keep getting undefined returned, however.但是,我不明白为什么我不断收到未定义的返回。 Could someone please show me the correct way to achieve what I'm trying to do?有人可以告诉我正确的方法来实现我想要做的事情吗? Thank you.谢谢你。

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. 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. This custom react hook is called: react-firebase-hooks You can find out more info here: https://www.npmjs.com/package/react-firebase-hooks Basically in your case the code that you need is this:这个自定义反应挂钩被称为: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>

and that's it, the user variable is an object that will contain all the informations that you'r looking for (id of user, email...)就是这样,用户变量是 object,它将包含您要查找的所有信息(用户 ID,email ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果js文件嵌入在另一个站点中,如何动态获取js文件中的用户ID - How can I get the user id in js file dynamically if the js file is embedded in another site 如何在Firebase中创建另一个唯一的用户ID? - How do you create another unique user id in firebase? 如何将函数从一个文件导出到另一个 node.js? - how to export a function from one file to another node.js? 如何将此值导出到另一个 JS 文件中? - How can I export this value into another JS file? 如何简单地将方法导出到要包含的另一个.js文件中<script> tag? - How to simply export a method to another .js file to be included with <script> tag? 如何将 mongodb db collections 导出到另一个 js 文件? - How to export mongodb db collections to another js file? 如何将获取请求参数导出到另一个JS文件 - How to export get request parameters to another JS file 如何将两个不同的功能从一个js文件导出到Protractor中的另一个Js文件 - how to export 2 different functions from one js file into another Js file in Protractor 如何正确地将一个普通的 JS 文件导出和导入到另一个 JS 文件中? - How do I correctly export and import a plain JS file into another JS file? 将数组导出到 Node.js 中的另一个文件 - Export array to another file in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM