1
votes

I have a firebase cloud function that triggers based on a new authentication that logs the user's information in the firebase real-time database. For some reason, the cloud function isn't triggering and the user is not getting registered in the database. This is the code for the cloud functions which I have tried to deploy

Index.js

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase);

const ref = admin.database().ref()

exports.creatUserAccount = functions.auth.user().onCreate(event => {
  const uid = event.data.uid
  const displayName = event.data.displayName
  const email = event.data.email
  const photoURL = event.data.photoURL || 'https://yt3.ggpht.com/-Sz69_iENQd4/AAAAAAAAAAI/AAAAAAAAAAA/zLg-bS6DJFo/s900-c-k-no-mo-rj-c0xffffff/photo.jpg'
  const phoneNumber = event.data.phoneNumber || ''
  const batchList = ['']

  const newUserRef = ref.child(`/users/students/${uid}`)
  return newUserRef.set({
    displayName: displayName,
    email: email,
    photoURL: photoURL,
    uid: uid,
    isDeleted: false,
    phoneNumber: phoneNumber,
    batchList: batchList,
  })
})

exports.deleteUserAccount = functions.auth.user().onDelete(event => {
  const uid = event.data.uid
  const userRef = ref.child(`users/students/${uid}`)
  return userRef.update({ isDeleted: true })
})

The firebase logs are empty and aren't helping me solve the problem

2

2 Answers

0
votes

Your code is fine. First, check the package version.

-- current
"firebase-admin": "^5.5.1",
"firebase-functions": "^0.7.5"
0
votes

If you are by any chance emulating Cloud Functions locally, the following url might be of use to you.

https://firebase.google.com/docs/functions/local-emulator

Basically, as explained in the documentation, you will have to generate a service account key, download the JSON file containing this key to your development workstation and create a System Variable GOOGLE_APPLICATION_CREDENTIALS that references the said JSON file.

As mentioned in the documentation, this service account key is essential if you wish to test out triggers locally that are not triggered by changes to database/cloud firestore.

Hope that helped.