TMac
TMac

Reputation: 117

Firebase: TypeError: Cannot read properties of undefined (reading 'getIdToken')

So I am running into an issue while working my way through a Firebase / React tutorial (https://www.youtube.com/watch?v=m_u6P5k0vP0&t=2656s). I am in the auth section trying to set up new users and I am having success in getting the new user into my Auth section on firebase but I am not getting their id token back so I can't move forward with creating a users collection programmatically. Currently I am seeing the TypeError on the command line and getting back a 500 error with an empty JSON string in Postman. Any ideas on what I am missing in data.user.getIdToken() ?

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

const firebaseConfig = {
  apiKey: "***",
  authDomain: "***",
  databaseURL: "***",
  projectId: "***",
  storageBucket: "***",
  messagingSenderId: "***",
  appId: "***",
  measurementId: "***"
};


admin.initializeApp(firebaseConfig);
const db = admin.firestore();


// First function is to get all screams (feed posts)
app.get('/screams', (req, res) => {
  db
    .collection('screams')
    .orderBy('createdAt', 'desc')
    .get()
    .then((data) => {
      let screams = [];
      data.forEach((doc) => {
        screams.push({
          screamId: doc.id,
          body: doc.data().body,
          userHandle: doc.data().userHandle,
          createdAt: doc.data().createdAt
        });
      });
      return res.json(screams);
    })
    .catch((err) => console.error(err))
})

// Second functions is to create ie post new screams from a json format
app.post('/screams', (req, res) => {

  const newScream = {
    body: req.body.body,
    userHandle: req.body.userHandle,
    createdAt: new Date().toISOString()
  };

  db.collection('screams').add(newScream).then((doc) => {
    res.json({ message: `document ${doc.id} created successfully` })
  })
  .catch((err) => {
    res.status(500).json({ error: 'something went wrong' });
    console.error(err);
  })
});

// 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
  };

  db.doc(`/users/${newUser.handle}`).get()
    .then( 
      doc => {
        if(doc.exists){
          return res.status(400).json({ handle: 'This handle already taken' });
        } else {
          return  admin
          .auth()
          .createUser({
            email: newUser.email,
            password: newUser.password,
          })
        }
      })
      .then(data => {
        data.user.getIdToken();
      })
      .then(token => {
        return res.status(201).json({ token });
      })
      .catch((err) => {
        console.error(err);
        return res.status(500).json({ error: err.code });
      });

});

// export api allows us to use express for our function formating
exports.api = functions.https.onRequest(app);
  TypeError: Cannot read properties of undefined (reading 'getIdToken')
      at /Users/tmac/Desktop/SMTutorial/functions/index.js:84:22
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

Upvotes: 3

Views: 9384

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

The createUser() method returns a UserRecord which has no user property. Also it seems you are trying to get a user's ID Token using Admin SDK which is not possible natively. Checkout In firebase - How to generate an idToken on the server for testing purposes? for more information.

When the user is created on server, you can simply send back a response and then log in the user using Firebase Client SDKs. Also try refactoring the code as shown below:

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

  const userDoc = await db.doc(`/users/${newUser.handle}`).get()
  if (userDoc.exists) {
    return res.status(400).json({ handle: 'This handle already taken'});
  }

  // Create user
  await admin.auth().createUser({ email: newUser.email, password: newUser.password })

  return res.json({ data: "User created" })
})

The flow on client side can be like this:

  1. User enters credentials and '/signup' API is triggered
  2. Log in user with the same credentials using Firebase client SDK after server returns successful response.

Upvotes: 3

Related Questions