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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With