Cloud Functions for Firebase - action on email verified

后端 未结 3 2178
滥情空心
滥情空心 2020-12-01 07:35

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 f

3条回答
  •  無奈伤痛
    2020-12-01 08:23

    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.

提交回复
热议问题