Reputation: 131
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?
Upvotes: 0
Views: 597
Reputation: 317750
You have a lot of things going wrong here that all need some correction. I'll list some here that I can immediately see:
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
).
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.
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.
Upvotes: 1