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?