I was trying to deploy the firebase-cloud function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const app = require("express")();
const { getAuth, createUserWithEmailAndPassword } = require("firebase/auth");
const auth = getAuth();
const firebase = require("firebase/app");
admin.initializeApp();
const config = {
apiKey: "",
authDomain: " ",
projectId: " ",
storageBucket: " ",
messagingSenderId: " ",
appId: " ",
measurementId: " ",
};
firebase.initializeApp(config);
//Signup Route
app.post("/signup", (req, res) => {
const newUser = {
email: req.body.email,
password: req.body.password,
};
createUserWithEmailAndPassword(auth, newUser.email, newUser.password)
.then((data) => {
return res
.status(201)
.json({ message: `user ${data.user.uuid} signed up successfully` });
})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: err.code });
});
});
exports.api = functions.https.onRequest(app);
But I gotthis error
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
CodePudding user response:
You seem to be trying to import the client-side JavaScript/Web SDK here:
const { getAuth, createUserWithEmailAndPassword } = require("firebase/auth");
const auth = getAuth();
const firebase = require("firebase/app");
That SDK is not support in Cloud Functions. Instead you should use the Admin SDK for Node.ks, which you're already importing too with:
const admin = require("firebase-admin");
To create a user with the Admin SDK:
admin
.auth()
.createUser({
email: '[email protected]',
emailVerified: false,
phoneNumber: ' 11234567890',
password: 'secretPassword',
displayName: 'John Doe',
photoURL: 'http://www.example.com/12345678/photo.png',
disabled: false,
})