Firebase - How to send email verification from onCreate cloud function listener? [duplicate] - firebase

This question already has answers here:
How to send email verification after user creation with Firebase Cloud functions?
(5 answers)
Closed 4 years ago.
I'm trying to create a cloud function that will listen for when users are created, and then send an email verification to the user that was created.
I have this code:
export const verifyEmail = functions.auth.user().onCreate((user) => {
});
The problem I have here is that user gives me no way of accessing sendEmailVerification. The only way of accessing that function is via the currentUser, but since this happens in a cloud function reaction to onCreate there is no current user.
I need to somehow get a firebase.User instance instead of a firebase.UserRecord, as the onCreate gives me.
How can I solve this?

There is no way to get the client-side user from the server, nor is there a way to send verification emails through the Admin SDK.
You will either have to trigger the email from the app itself, or send your own verification email. In the latter case, you can still use the existing verification service, by generating the correct email verification link.
Also see How to send email verification after user creation with Firebase Cloud functions?, which I'll actually mark your question as a duplicate of.

Related

How to trigger a cloud function in Firebase on password change?

I am trying to trigger a Firebase cloud function when a user changes their password, be it by
changing the password (firebase.auth().currentUser. updatePassword(newPassword) ) or by reseting it (firebase.auth(). sendPasswordResetEmail(email) ). Since I am not storing password anywhere I can not use .onUpdate trigger (or any other of the triggers).
I found a similar question, but it only asked about a trigger on password change and there is no info about a workaround: Firebase cloud function listener for password change
Edit: with this trigger I want to send an email to user that their password has been changed.
Does anyone have any ideas? Is it possible at all?
What you want is totally possible, but you'll have to implement it yourself. As the link you found states, there is no built-in on-password-update account trigger... only onCreate and onDelete account triggers. Which means we have to handle it manually. I would handle it the same way you are heading - using a cloud function to send the user an email.
I would build a cloud function named something like notifyUserOfPasswordChange() and call that cloud function from your app immediately after the line of code where you call .updatePassword() or .confirmPasswordReset() (which is the finishing step after .sendPasswordResetEmail()). If I understand the point of your question - this is the real answer here. You will need to call a cloud function manually whenever you execute password update code. There's no automated trigger.
The email can be as simple or customized as you code it. If you're unsure, then start simple and have the cloud function get the target email address from the data parameter - and then use a generic message for the email body. If you feel more adventurous, consider passing the user's UID and using the Admin SDK to look up that user's registered email address & display name and then building a prettier & personalized HTML email.
As far as how to send an email from Firebase cloud functions, there are plenty of examples out there - a good sample is on Firebase's GitHub page.
Workaround
If you're using Firestore, you can use Cloud Firestore triggers as a workaround.
Step 1
Create a Cloud Function called sendEmail.
// This cloud function will get triggered when a new document is added to `emails` collection.
exports.sendEmail = functions.firestore
.document('emails/{documentId}')
.onCreate(async (snapshot, context) => {
const data = snapshot.data()
const user = data.user
if (data.changedPassword == true) {
// send email to the user saying password was changed
} else if (data.changedEmail == true) {
// send email to the user saying the email address was changed
}
})
Step 2
Write a document to your emails collection anytime the user updates their password.
// After you call .updatePassword() or .confirmPasswordReset()
// then you do this
const db = firebase.firestore();
db.collection("emails").add({
user: {INSERT USER ID HERE}",
changedPassword: true, //changedEmail: true,
})
.then((docRef) => {
// handle success
})
.catch((error) => {
// handle error
});
(Optional) Step 3
Write a Firebase Rule to protect the emails collection. The rule says: "Only allow documents to be written to the emails collection if the user field matches the authenticated user."
Summary
On the client, you'll write a new document to the emails collection anytime you want to send an email. The document will include fields for the user id and the type of email (e.g. changedPassword, changedEmail). Your cloud function will automatically send emails when documents are added to the emails collection. Firestore Rules will ensure emails are sent only to the intended user.
This system is reusable for any type of email you want to send the user. In my app, I send emails to the user when their password is changed and when their email is changed.

email delivery report in Firebase

I want to know whether the email is delivered to the respective email account or not Using Firebase Auth API's
Let me know if there is any workaround ?
I assume you are referring to the Firebase Authentication. The user is retrieved from mAuth.getCurrentUser() method, and the sendEmailVerification() method is called on it. This gets Firebase to send the mail, if the task was successful, you'll see that a toast will pop up showing that it happened.
sendEmailVerification()

Customize Firebase email template [duplicate]

This question already has answers here:
Firebase authentication email customisation
(4 answers)
Closed 1 year ago.
When a user creates an account using firebase, I use the firebaseUser.sendVerficiationEmail() to send a verification link to the user. It works perfectly. However, the problem is that the email looks unprofessional and ugly. I know apps that use Firebase, such as "pic-collage", and their verification email looks pretty with their app logo on the top, and the verification link is a button.
Firebase template doesn't allow me to customize anything other than 'sender name' and 'action url'. Any help would be appreciated.
I checked and it does not seem that this is available as a ready made feature. So i am guessing the other companies you mentioned having customized verification emails have had to code them.
For that you could create a Firebase cloud functions listening for auth.onUser().user().onCreate(user), from that function you would send a custom email to the user with a link to another Firebase cloud function that one would be an https.onRequest which would complete the email verification process on it's invocation.

Enabling the notification feature for my firebase app

I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events

Firebase cloud function send notification to all users

I am trying to send a notification to all users of my app whenever there is a write on the database.
Here is the code that I have so far.
export const newUserAdded = functions.database
.ref('1_0_0/updateDate/date')
.onWrite((event)=>{
//send notfigication to all
admin.messaging().sendToCondition()
})
As you can see whenever there is a write to the date key the function will trigger.
But note here that I do not have device token of the user as I want to send a notification to all users. I was not able to find any method do so.
Kindly do let me know how can I achieve this.
Thanks
You can use topic messaging. Arrange for all your client apps to subscribe to a dedicated, named topic for broadcasting to all app installations, then send your message to that topic.

Resources