Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling sendPasswordResetEmail() within Cloud Functions for Firebase

I know Cloud Functions for Firebase is still pretty new, but I'm trying to move some client code into the cloud, and the 'signup' process seemed like an obvious target.

Currently, the signup page asks for an email address, generates a random password and calls createUserWithEmailAndPassword(). This works fine, and I then want to send the 'reset password' email to the email address that was just used, both to confirm the email address, and give the user a chance to set a specific password of their choice. I'm able to do this in the client code, but I wanted to experiment with creating a function triggered by onCreate().

That bit is working fine, but I can't figure out how to call sendPasswordResetEmail() from within the firebase functions environment.

This is the code so far:

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

exports.createUserRec = functions.auth.user().onCreate(event => {
  admin.auth().sendPasswordResetEmail(event.data.email)
})

The function log says:

TypeError: admin.auth(...).sendPasswordResetEmail is not a function

I'm assuming the auth() object is not the same auth() object that is found in the client side SDK, where this would work:

import * as firebase from 'firebase/app'
import 'firebase/database'
import 'firebase/auth'

var fbConfig = {
  // All the required bits
}

firebase.initializeApp(fbConfig)
.
.
.
firebase.auth().sendPasswordResetEmail(emailAddress)

I briefly searched the firebase functions source code, and I couldn't see any obvious reference to the sendPasswordResetEmail() function, so perhaps it's not directly available (yet?). Is there any other way to trigger this - apart from just putting it back in the client code?

like image 851
dsl101 Avatar asked Apr 04 '17 18:04

dsl101


People also ask

How to call a Cloud function Firebase?

The Cloud Functions for Firebase client SDKs let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app.

What is the difference between onCall HTTP callable and onRequest HTTP request functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


1 Answers

OK, I think I finally found the answer to my specific question - the 2 objects returned by the auth() call are not the same, as can be seen here:

  • Client side SDK Auth object methods include sendPasswordResetEmail()
  • Server side SDK Auth object methods don't include sendPasswordResetEmail()

I'm hoping as time passes these 2 APIs will converge slightly, but for now it seems you can't initiate a password reset from a Cloud Function.

like image 129
dsl101 Avatar answered Sep 23 '22 14:09

dsl101