0

I am trying to create a user in firebase using its getAuth() command. When I start my Node.js server side code I get an error:

TypeError: Cannot read properties of undefined (reading 'getProvider') at Object._getProvider (/Users/mnjuhn/Projects/CloudMedic/src/routes/functions/node_modules/@firebase/app/dist/index.cjs.js:276:10)
    at getAuth (/Users/mnjuhn/Projects/CloudMedic/src/routes/functions/node_modules/@firebase/auth/dist/node/totp-fe65684a.js:8197:24)
    at Object.<anonymous> (/Users/mnjuhn/Projects/CloudMedic/src/routes/functions/app.js:26:14)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/Users/mnjuhn/Projects/CloudMedic/src/routes/functions/user.js:2:36)

Here is my user and firebase app code.

var functions = require("firebase-functions");
const { getDatabase, ref, set, child, push, update } = require("firebase/database");
const { getAuth, onAuthStateChanged, signInWithEmailAndPassword, initializeAuth, createUserWithEmailAndPassword } = require("firebase/auth");
const { doc, setDoc } = require('firebase/firestore');
const { getFirestore, Timestamp, FieldValue, Filter } = require('firebase-admin/firestore');
const { initializeApp } =  require('firebase-admin/app');
const http = require('http');
const express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
var cors = require('cors');

const firebaseConfig = {
  // hidden configuration code
};

const admin = initializeApp(firebaseConfig);
const db = getFirestore(admin);
const auth = getAuth(admin);

const app = express();
app.use(cors());

app.use(bodyParser.json());
app.use(bodyParser.json({limit:'5mb'}));
app.use(bodyParser.urlencoded({extended:true}));
var jsonParser = bodyParser.json();

async function signIn(email, password) {

  signInWithEmailAndPassword(email, password).then((userCredential) => {
     // Signed in
     console.log("TEST Signed in");
     return userCredential;
   }).catch((error) => {
     const errorCode = error.code;
     const errorMessage = error.message;
     console.log("Error Signing in");
     console.log(error.message);
     throw error;
   });
}

exports.db = db;
exports.app = app;
exports.signIn = signIn;
exports.admin = admin;
exports.auth = auth;
exports.createUserWithEmailAndPassword = createUserWithEmailAndPassword;

Here is my create user code.

var constants = require('./constants.js');
const { db, app, signIn, admin } = require('./app');
var cors = require('cors');
const { Timestamp } = require('firebase/firestore');

function signup(req, res) {
    birthDate = new Date(Number(req.query.birthDate));
    var user = {
         id: db.collection("users").doc().id,
         email: req.query.email, 
         password: req.query.password,
         firstName: req.query.firstName,
         lastName: req.query.lastName,
         gender: req.query.gender,
         birthDate: birthDate,
         timestamp: new Date()
   };

    const auth = app.auth;
    app.createUserWithEmailAndPassword(auth, req.query.email, req.query.password).then((userCredential) => {
       // Signed up
       const user = userCredential.user;
       console.log("USER SUCCESSFULLY CREATED");
      }).catch((error) => {
        const errorCode = error.code;
        const errorMessage = error.message;
        console.log("ERROR CREATING USER");
        // ..
      });

      return db.collection('users').doc(user.id).set(user).then(snapshot => {
          console.log(user);
          return res.status(200).json({status:200, message:"ADDED USER"});
      }).catch(err => {
          console.log(err);
          return res.status(200).json({status:200, message:"ERROR ADDING USER"});
      });
}

module.exports = {
  signup: signup,
};

Could this error which stems from the line const auth = getAuth(admin); be the result of using firebase admin?

1 Answer 1

1

You have a lot of things going wrong here that all need some correction. I'll list some here that I can immediately see:

  1. You're mixing up use of firebase-admin with the Firebase web client libraries (firebase/auth). They're not compatible with each other. You can't use the web client getAuth with an app initialized with the Firebase Admin SDK (firebase-admin/app).

  2. The Firebase web client firebase/auth isn't intended for use on backend systems at all. It's not supported for a backend to sign in using signInWithEmailAndPassword. That's for web frontends only. Users sign into frontend applications, not backends. Backends just respond to requests issues by authenticated users, and they are identified by the token provided in the request. The Firebase Admin documentation has an example of this.

  3. These exports make no sense for use with Cloud Functions. You are only supposed to export the individual functions for deployment.

exports.db = db;
exports.signIn = signIn;
exports.admin = admin;
exports.auth = auth;
exports.createUserWithEmailAndPassword = createUserWithEmailAndPassword;

If you are using an express app, you just export that. I suggest reviewing the documentation on that more carefully. You should first get a simple express app to work, then add functionality to it.

7
  • Thanks for your help! Still a bit new to firebase and express.
    – MattJ
    Feb 6 at 20:12
  • I am trying to sort out the difference between firebase auth and firebase admin. In my case where I wish to allow users to signup and then login using their provided email and password. Since I want to use the Firebase Admin SDK should I only have one admin user to read and write to my firebase DB. And then all users who signup should I be writing their user info into in a users table in firestore? And then authenticate the user based on tokens?
    – MattJ
    Feb 7 at 0:08
  • Firebase Admin is for backend code that can securely work with Firebase services. It's not for "admin users". That is an entirely different concept. If you want to grant certain users the ability to do things that other cannot, you need to restrict that access using backend code you write that checks their permissions. Feb 7 at 0:37
  • Here is a similar use case - stackoverflow.com/questions/54173320/… Are these best practices?
    – MattJ
    Feb 7 at 0:49
  • That post has nothing to do with firebase-admin and seems completely unrelated to your question here. Feb 7 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.