0

I am following the following github to test authentication on firebase on node.js, https://github.com/hvalls/node-firebase-auth-example

I am getting the firebase.auth() is not a function.

I have tried the following:

  1. Remove Firebase-admin
  2. Remove Firebase
  3. Reinstall Firebase via Npm

I just cant figure out whats wrong.

user_service.js const firebaseapp = require("firebase/app"); require("firebase/auth"); const apiKey = xxxx const fb = firebaseapp.initializeApp({ apiKey: apiKey, }); exports.addUser = (email, password) => fb.auth().createUserWithEmailAndPassword(email, password); exports.authenticate = (email, password) =>{ fb.auth().signInWithEmailAndPassword(email, password); } 

Route.js

router.post('/now', function (req, res, next){ console.log('Here'); try { const u = userService.authenticate(req.body.email, req.body.password); } catch (error) { console.log("Error : " + error); req.sendStatus(500).send(error); } }); 

1 Answer 1

1

You should ideally use the Admin SDK on server. The error you are getting might be because you've installed Modular SDK (v9.0.0+) which does not use the name-spaced syntax. Try installing Admin SDK (firebase-admin) and refactoring the code as shown below:

router.post('/user', async function (req, res, next){ try { const newUser = await userService.addUser(req.body.email, req.body.password); return res.json(newUser) } catch (error) { console.log("Error : " + error); req.sendStatus(500).send(error); } }); 
const admin = require("firebase-admin"); admin.initializeApp({ // service account }); exports.addUser = async (email, password) => { return admin.auth().createUser({ email, password }); } 

You cannot login user using Admin SDK (unless you are using session based auth) so after the user is created you should log the user in with signInWithEmailAndPassword() on your client application using client SDK.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.