Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

node.js - Cloud Functions for Firebase - action on email verified

Im trying to create a Cloud Function trigger that will execute after email has been verified.

In the Cloud Functions samples I could only find examples on triggers for onCreate and onDelete.

Within the documentation I found something about creating custom action handlers but I don't actually want to replace the standard email verification dialog they have by default, I just want to change the property of a "user" after the email is verified.

Does anyone have any experience with this, and is this even possible? Or is my only option to create my custom verification view/dialog webpage?

question from:https://stackoverflow.com/questions/43503377/cloud-functions-for-firebase-action-on-email-verified

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I faced this problem and took me a long time to figure it out how to solve so I hope this could help anyone that could get stuck into this too:

1 -> I created a function that was triggered with onCreate() for a new user

exports.sendConfirmationEmail = functions.auth.user()
                    .onCreate((user) => {
                        const actionCodeSettings = {
                            url: 'https://appNextURL.com/',
                            handleCodeInApp: false//ensure that the link will open into browser
                        };
                        return admin.auth().generateEmailVerificationLink(user.email, actionCodeSettings)
                            .then(async (link) => {
                                await db.collection('users').doc(user.uid).set({
                                    verificationLink: link,
                                    emailVerified: false
                                }, {merge: true});
                                return sendCustomVerificationEmail(user.email, user.displayName, link);
                            })
                            .catch((err) => {
                                console.error("Error:", err);
                                return Promise.reject(err);
                            });
                    });
  • The generateEmailVErificationLink() will generate the link based on the link we will save on step 3.

  • The function sendCustomVerificationEmail() is just an internal function that overcomes the standard email firebase send

2 -> Then I created a function that will receive a manual http trigger with the data that would be generated automatically by firebase when sending an automatic email

exports.verifyEmail = functions.https.onRequest((req, res) => {
                        const {mode, oobCode, apiKey, continueUrl, lang} = req.query;
                        const link = "https://us-central1-projectId.cloudfunctions.net/verifyEmail/?mode=" + encodeURIComponent(mode) + "&oobCode=" + encodeURIComponent(oobCode) + "&apiKey=" + encodeURIComponent(apiKey) + "&continueUrl=" + encodeURIComponent(continueUrl) + "&lang=" + encodeURIComponent(lang);
                        return db.collection("users")
                            .where("verificationLink", "==", link)
                            .get()
                            .then(function (querySnapshot) {
                                querySnapshot.forEach(function (user) {
                                    const userData: UserData = user.data();
                                    console.log("email verified: ", userData.userId);
                                    return admin.auth().updateUser(userData.userId, {
                                        emailVerified: true
                                    }).then(function (userRecord) {
                                        return db.collection('users').doc(userData.userId).set({emailVerified: true}, {merge: true});
                                    });
                                });
                                return res.sendStatus(200).end();
                            }).catch(function (err) {
                                console.log("error:", err);
                                return res.sendStatus(403).end();
                            });
                    });
  • As I saved the link in the onCreate() I can now query that link to get who is the user that I am authenticating

3 -> the third step is to change the link in to Firebase Authentication template to the link generated into the 2nd step:

Navigate to Authentication>Templates:

  • Click on edit icon> Click on customize action URL:

  • Navigation

  • Paste the link generated into the step 2 and save:

  • Save link

Now every link generated automatically will go trought that function you created on step 2 and you will be able to handle the actions you want to happen.

I hope I could be clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...