Reputation: 1784
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?
Upvotes: 5
Views: 4570
Reputation: 1784
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:
sendPasswordResetEmail()
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.
Upvotes: 12
Reputation: 11807
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('[email protected]')
Then you can email that link using your own email service.
Upvotes: 4