5

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?

AL.
  • 36,815
  • 10
  • 142
  • 281
dsl101
  • 1,715
  • 16
  • 36

2 Answers2

12

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:

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.

dsl101
  • 1,715
  • 16
  • 36
3

Another solution is to generate the reset password link, then email it. This can be done in a cloud function like so:

const resetLink = await admin.auth().generatePasswordResetLink('user@example.com')

Then you can email that link using your own email service.

Ben Winding
  • 10,208
  • 4
  • 80
  • 67
  • This should be the accepted answer, it's even preferred – OZZIE Mar 17 '21 at 10:38
  • Since my question was really about ways to do this in the admin SDK, this doesn't really _answer_ it as opposed to provide an alternative. Mine answers it in so far as showing the 2 SDKs are not equivalent. – dsl101 Mar 17 '21 at 14:50
  • @dsl101 ok but it answers my question, which is the most important :D I'm just kidding :P – OZZIE Mar 17 '21 at 15:07
  • @dsl101 is this related to the topic here? https://stackoverflow.com/questions/66675374/firebase-magic-email-link-together-with-password-login – OZZIE Mar 17 '21 at 15:18
  • Well, I guess so, but how much I don't know I'm afraid. We're using firebaseui with only google and username/password providers. I haven't experimented with email link signin yet, and thankfully our password reset requirements are so low we can just do it via the console, so I didn't bother with getting the email password reset to work in the end... – dsl101 Mar 17 '21 at 19:44