0
votes

I am setting a new firebase project. In the project I try to use firebase.auth() to create a new user with user email and password. However, when I use firebase serve and do a post request with the link in postman, i get an error which says firebase.auth is not a function.

I believe that there are questions regarding this issue, however I tried all the solutions that they have provided but none of the worked for me.

I tried: - Adding require firebase/auth - Deleting node modules and reinstalling firebase and firebase functions - Import firebase and functions in different order - Install firebase and functions in a different order - Create a new project and install firebase and functions from scratch

const firebase = require('firebase');
const config = {
    apiKey: "xxxxx,
    authDomain: "xx",
    databaseURL: "xxxx",
    projectId: "xxxx",
    storageBucket: "xxxx",
    messagingSenderId: "xxx",
    appId: "xxxxxx"
};

firebase.initializeApp(config);

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const app = require('express')();

admin.initializeApp();

//Signup route

app.post('/signup', (req, res) => {
    const newUser = {
        email: req.body.email,
        password: req.body.password,
        confirmPassword: req.body.confirmPassword,
        handle: req.body.handle,
    }

    // TODO: validate data

    firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
    .then(data => {
        return res.status(201).json({ message: `user ${data.user.uid} signed up successfully`})
    })
    .catch(err => {
        console.error(err);
        return res.status(500).json({error: err.code});
    });
});

Expected results: get status 201 on postman and created new user in firebase

Actual results: TypeError: firebase.auth is not a function. In the console and postman.

3
Pls check out another thread here. It may help. - Mohammedsalim Shivani

3 Answers

1
votes

You should understand the difference of Firebase JavaScript SDK and Firebase Admin SDK for Node.

The Firebase JavaScript SDK is for the client side.

Firebase Admin SDK is for the server side(like the Cloud Functions).

So in your case, you should use admin.auth().createUser() .

See:

0
votes

It seems you want to instantiate the application using the client sdk instead of the server.

You need to use a private key instead, to get one go to your firebase console -> Project Overview -> Service accounts -> Generate new private key

then after you download and include the key in your project:

const admin = require('firebase-admin')
const serviceAccount = require('your_firebase_key/path/goes/here')
admin.initializeApp({
   credential: admin.credential.cert(serviceAccount)
})

Good luck!

0
votes

I was able to resolve this issue by installing firebase as well as firebase-tools. I had initially only installed firebase-tools. Try running npm i firebase, restart the server firebase serve and see if you're still having the issue.