在 app.js 中导入 firebase 后,我进行了初始化,但在控制器中 firebase.auth.createUserWithEmailAndPassword 不是函数

After importing firebase in app.js I initialized but in controller firebase.auth.createUserWithEmailAndPassword is not a function

这是在我的 app.js 中导入 firebase 并对其进行初始化的地方

    const firebase = require('firebase');

    firebase.initializeApp({
      apiKey: ************,
      authDomain: ****************,
      databaseURL: **********",
      projectId: ********,
      storageBucket: ***********,
      messagingSenderId: **********,
      appId: *********,
      measurementId: *********
    });

    app.use( (req, res, next) => {
      firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
            req.session.isLoggedIn = true;
        } else {
            req.session.isLoggedIn = false;
        }
      });
      res.locals.isAuthed = req.session.isLoggedIn;
      res.locals.csrfToken = req.csrfToken();
      next();
    });

但在 auth.js 控制器中导入 firebase 后它将无法工作

    const firebase = require('firebase');

    exports.postSignup = (req, res, next) => {
     const name = req.body.name;
     const email = req.body.email;
     const password = req.body.password;
     const confirmPassword = req.body.password;

     if(password === confirmPassword){
        
        firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(function(firebaseUser) {
            const user = new User({
                name: name,
                email: email,
                cart: {items: []}
            });
            return user.save();
        })
        .then(result => {
            res.redirect('/login');
        })
        .catch(error => {
            if (error.code == 'auth/email-already-in-use') {
                //'auth/wrong-password'
                req.flash('error', 'Already Registered');
                return res.redirect('/signup');
            }
        });  
    }

出现这个错误 类型错误:firebase.auth.createUserWithEmailAndPassword 不是一个函数 在 exports.postSignup (/media/rishav/Project Space/Projects/Web Development/Node Express/Controllers/auth.js:95:23)

    const firebase = require('firebase-admin');

    // Fetch the service account key JSON file contents
    const serviceAccount = require("../Data/firebase-service-account.json");

    // Initialize the app with a service account, granting admin privileges
    firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccount),
      databaseURL: "****",
      databaseAuthVariableOverride: null
    });

你可能想说:

firebase.auth().createUserWithEmailAndPassword(...)

注意“auth”后面的括号。

但更大的问题是您不应该在 nodejs 应用程序中使用 Firebase 网络客户端库。相反,您应该在后端代码中使用 Firebase Admin SDK, which is an entirely different module, and runs with admin privileges initialized by a service account. You can use that to create user accounts