简体   繁体   中英

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

This is in my app.js where I import firebase and initialized it

    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();
    });

But in auth.js Controller after importing firebase it wont work

    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');
            }
        });  
    }

This error occurs TypeError: firebase.auth.createUserWithEmailAndPassword is not a function at 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
    });

You probably meant to say:

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

Notice the parenthesis after "auth".

But the bigger issue is that you shouldn't use the Firebase web client library in your nodejs application. Instead, you should use the 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 in your backend code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM