Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Data to Firebase auth.user().onCreate Cloud Function

I'm testing out firebase cloud functions and I'm curious as to how to pass data from my form to the cloud function. I've created a registration form that accepts several fields - my plan is to use firebase to create an auth account and have a cloud function that runs whenever a auth account is created. This function will create a user document inside Firestore.

Code below gives an idea of my frontend setup


import React from "react"
import firebase from "firebase"

// create some config for firebase

const app = firebase.initializeApp(config)
const auth = app.auth()

function Form({onSubmit}){

  function handleSubmit(e){
    e.preventDefault()
    
    const {email, password, firstName, lastName, age} = e.target.elements
    const user = {email: email.value, password: password.value, firstName: firstName.value, lastName: lastName.value, age: age.value}
  
    onSubmit().then(() => {}, (err) => {})
  }
  
  return <form onSubmit={handleSubmite}>
     {/*form fields..*/}
  </form>
}

// Register function for firebase

function register(email, password){
  return auth.createUserWithEmailAndPassword(email, password)
}

// Parent for form component
function Container(){

  return <Form onSubmit={register}/>
}

This is what my cloud function looks like:

const functions = require("firebase-functions")
const admin = require("firebase-admin")

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

const createUser = (userData, context) => {
  const { email } = userData
  console.log(userData)

  return db
    .collection("users")
    .doc()
    .set({
      email,
      firstName: "TEST",
    })
    .catch(console.error)
}

module.exports = {
  authOnCreate: functions.auth.user().onCreate(createUser),
}

Any idea how to do this? Am I thinking about this incorrectly? I'm a bit new to firebase.

TLDR; How do I send custom data from a form to a cloud function that will create a user in firestore on a new user being created in auth?

like image 554
Dowen Robinson Avatar asked Dec 01 '25 09:12

Dowen Robinson


1 Answers

my plan is to use firebase to create an auth account and have a cloud function that runs whenever a auth account is created. This function will create a user document inside Firestore.

This is not possible. Since your Cloud Functions triggers on auth.user().onCreate it only has access to the information that is available with that event. You can't pass additional information to a background function.

The most common solutions are:

  1. Write the document after the user is created, either directly from the client or by having the client call another Cloud Function directly.
  2. Pass all data that is needed to create the user and document to a Cloud Function, and then let the Cloud Function handle both the creation of the user account and the document.
like image 89
Frank van Puffelen Avatar answered Dec 04 '25 01:12

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!