Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test my firebase auth triggers locally

I would like to test some firebase database and auth triggers locally and have found this documentation https://firebase.google.com/docs/functions/local-emulator#invoke_realtime_database_functions.

Following along the instructions I typed

firebase functions:shell

and then invoked my custom firebase trigger with

createNewUser({user: {email: '[email protected]'}},{auth: {uid: '123'}})

However, this gives me the error

firebase > createNewUser({user: {email: '[email protected]'}},{auth: {uid: '123'}})
'Successfully invoked function.'
firebase > info: User function triggered, starting execution
info: Function crashed
TypeError: Cannot read property 'toLowerCase' of null

This is the function I try to run:

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

exports.createNewUser = functions.auth.user().onCreate((user) => {
  let lowerCaseEmail = user.email.toLowerCase();
  return admin.database().ref(`/users/${user.uid}/lowerCaseEmail`).set(lowerCaseEmail);
});

I believe I don't invoke my custom function correctly in firebase functions:shell.

Would someone like to point me in the right direction?

like image 681
Albert Avatar asked Oct 24 '25 19:10

Albert


1 Answers

As per the documentation you have to pass a UserRecord (see doc) when invoking an Auth function, see at the bottom of the doc you point to in your Question (i.e. https://firebase.google.com/docs/functions/local-emulator#invoke_realtime_database_functions)

Doing the following works:

createNewUser({ disabled: false, 
                displayName: 'Renaud', 
                email: '[email protected]', 
                emailVerified: false, 
                metadata: {creationTime: null, lastSignInTime: null}, 
                photoURL: null, 
                providerData: ['google.com'], 
                uid: '123' })

You do find the new record in the database, with the email in lower case.


Actually, since the documentation says: "Specify only the fields that your code depends on, or none at all if you only want to run the function.", I've tried with the following UserRecord object, and it also works.

createNewUser({email: '[email protected]', uid: '123'})
like image 172
Renaud Tarnec Avatar answered Oct 26 '25 08:10

Renaud Tarnec



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!