पसंद के मुताबिक ईमेल कार्रवाई हैंडलर बनाएं

कुछ यूज़र मैनेजमेंट कार्रवाइयों की वजह से, उपयोगकर्ता को ईमेल भेजे जाते हैं. जैसे, उपयोगकर्ता का ईमेल पता अपडेट करना और उसका पासवर्ड रीसेट करना. इन ईमेल में ऐसे लिंक होते हैं जिन्हें खोलकर, उपयोगकर्ता को मैनेज करने की कार्रवाई पूरी की जा सकती है या उसे रद्द किया जा सकता है. डिफ़ॉल्ट रूप से, उपयोगकर्ता मैनेजमेंट के ईमेल, डिफ़ॉल्ट ऐक्शन हैंडलर से लिंक होते हैं. यह आपके प्रोजेक्ट के Firebase होस्टिंग डोमेन में मौजूद यूआरएल पर होस्ट किया गया वेब पेज होता है.

इसके बजाय, कस्टम प्रोसेसिंग करने और ईमेल ऐक्शन हैंडलर को अपनी वेबसाइट के साथ इंटिग्रेट करने के लिए, कस्टम ईमेल ऐक्शन हैंडलर बनाया और होस्ट किया जा सकता है.

उपयोगकर्ता को उपयोगकर्ता मैनेजमेंट से जुड़ी ये कार्रवाइयां पूरी करने के लिए, ईमेल ऐक्शन हैंडलर का इस्तेमाल करना होगा:

  • पासवर्ड रीसेट करना
  • ईमेल पते में किए गए बदलावों को रद्द करना—जब उपयोगकर्ता अपने खातों के मुख्य ईमेल पते बदलते हैं, तो Firebase उनके पुराने पतों पर एक ईमेल भेजता है. इससे वे बदलाव को पहले जैसा कर सकते हैं
  • ईमेल पतों की पुष्टि करना

अपने Firebase प्रोजेक्ट के ईमेल ऐक्शन हैंडलर को पसंद के मुताबिक बनाने के लिए, आपको एक वेब पेज बनाना और होस्ट करना होगा. यह वेब पेज, अनुरोध की पुष्टि करने और उसे पूरा करने के लिए, Firebase JavaScript SDK का इस्तेमाल करता है. इसके बाद, आपको अपने Firebase प्रोजेक्ट के ईमेल टेंप्लेट को पसंद के मुताबिक बनाना होगा, ताकि उन्हें कस्टम ऐक्शन हैंडलर से लिंक किया जा सके.

ईमेल ऐक्शन हैंडलर पेज बनाना

  1. जब Firebase, यूज़र मैनेजमेंट के लिए ईमेल जनरेट करता है, तब वह आपके ऐक्शन हैंडलर यूआरएल में कई क्वेरी पैरामीटर जोड़ता है. उदाहरण के लिए:

    https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy...&lang=fr

    इन पैरामीटर से, यूज़र मैनेजमेंट के उस टास्क के बारे में पता चलता है जिसे उपयोगकर्ता पूरा कर रहा है. आपके ईमेल ऐक्शन हैंडलर पेज को इन क्वेरी पैरामीटर को हैंडल करना चाहिए:

    पैरामीटर
    मोड

    यूज़र मैनेजमेंट से जुड़ी वह कार्रवाई जिसे पूरा करना है. इनमें से कोई एक वैल्यू हो सकती है:

    • resetPassword
    • recoverEmail
    • verifyEmail
    oobCode एक बार इस्तेमाल होने वाला कोड, जिसका इस्तेमाल किसी अनुरोध की पहचान और पुष्टि करने के लिए किया जाता है
    apiKey आपके Firebase प्रोजेक्ट का एपीआई पासकोड, जो आपकी सुविधा के लिए दिया गया है
    continueUrl यह एक वैकल्पिक यूआरएल है. इसकी मदद से, यूआरएल के ज़रिए ऐप्लिकेशन को स्थिति की जानकारी वापस भेजी जा सकती है. यह पासवर्ड फिर सेट करने और ईमेल पते की पुष्टि करने के मोड के लिए ज़रूरी है. पासवर्ड रीसेट करने या पुष्टि करने के लिए ईमेल भेजते समय, ActionCodeSettings ऑब्जेक्ट की जानकारी देना ज़रूरी है. साथ ही, इसके लिए एक कंटिन्यू यूआरएल भी देना होगा. इससे उपयोगकर्ता, ईमेल पर कोई कार्रवाई करने के बाद, वहीं से शुरू कर सकता है जहां उसने छोड़ा था.
    lang

    यह वैकल्पिक BCP47 भाषा टैग है, जो उपयोगकर्ता की भाषा और देश/इलाके की जानकारी देता है (उदाहरण के लिए, fr). इस वैल्यू का इस्तेमाल करके, उपयोगकर्ताओं को उनकी स्थानीय भाषा में ईमेल ऐक्शन हैंडलर पेज उपलब्ध कराए जा सकते हैं.

    स्थानीय भाषा को Firebase कंसोल से सेट किया जा सकता है. इसके अलावा, ईमेल ट्रिगर करने से पहले, उससे जुड़े क्लाइंट एपीआई को कॉल करके भी इसे डाइनैमिक तरीके से सेट किया जा सकता है. उदाहरण के लिए, JavaScript का इस्तेमाल करना: firebase.auth().languageCode = 'fr';.

    उपयोगकर्ताओं को एक जैसा अनुभव देने के लिए, पक्का करें कि ईमेल ऐक्शन हैंडलर का स्थानीय भाषा में अनुवाद, ईमेल टेंप्लेट से मेल खाता हो.

    यहां दिए गए उदाहरण में, ब्राउज़र पर आधारित हैंडलर में क्वेरी पैरामीटर को मैनेज करने का तरीका बताया गया है. (इसी तरह के लॉजिक का इस्तेमाल करके, हैंडलर को Node.js ऐप्लिकेशन के तौर पर भी लागू किया जा सकता है.)

    Web

    import { initializeApp } from "firebase/app";
    import { getAuth } from "firebase/auth";
    
    document.addEventListener('DOMContentLoaded', () => {
      // TODO: Implement getParameterByName()
    
      // Get the action to complete.
      const mode = getParameterByName('mode');
      // Get the one-time code from the query parameter.
      const actionCode = getParameterByName('oobCode');
      // (Optional) Get the continue URL from the query parameter if available.
      const continueUrl = getParameterByName('continueUrl');
      // (Optional) Get the language code if available.
      const lang = getParameterByName('lang') || 'en';
    
      // Configure the Firebase SDK.
      // This is the minimum configuration required for the API to be used.
      const config = {
        'apiKey': "YOUR_API_KEY" // Copy this key from the web initialization
                                 // snippet found in the Firebase console.
      };
      const app = initializeApp(config);
      const auth = getAuth(app);
    
      // Handle the user management action.
      switch (mode) {
        case 'resetPassword':
          // Display reset password handler and UI.
          handleResetPassword(auth, actionCode, continueUrl, lang);
          break;
        case 'recoverEmail':
          // Display email recovery handler and UI.
          handleRecoverEmail(auth, actionCode, lang);
          break;
        case 'verifyEmail':
          // Display email verification handler and UI.
          handleVerifyEmail(auth, actionCode, continueUrl, lang);
          break;
        default:
          // Error: invalid mode.
      }
    }, false);

    Web

    document.addEventListener('DOMContentLoaded', () => {
      // TODO: Implement getParameterByName()
    
      // Get the action to complete.
      var mode = getParameterByName('mode');
      // Get the one-time code from the query parameter.
      var actionCode = getParameterByName('oobCode');
      // (Optional) Get the continue URL from the query parameter if available.
      var continueUrl = getParameterByName('continueUrl');
      // (Optional) Get the language code if available.
      var lang = getParameterByName('lang') || 'en';
    
      // Configure the Firebase SDK.
      // This is the minimum configuration required for the API to be used.
      var config = {
        'apiKey': "YOU_API_KEY" // Copy this key from the web initialization
                                // snippet found in the Firebase console.
      };
      var app = firebase.initializeApp(config);
      var auth = app.auth();
    
      // Handle the user management action.
      switch (mode) {
        case 'resetPassword':
          // Display reset password handler and UI.
          handleResetPassword(auth, actionCode, continueUrl, lang);
          break;
        case 'recoverEmail':
          // Display email recovery handler and UI.
          handleRecoverEmail(auth, actionCode, lang);
          break;
        case 'verifyEmail':
          // Display email verification handler and UI.
          handleVerifyEmail(auth, actionCode, continueUrl, lang);
          break;
        default:
          // Error: invalid mode.
      }
    }, false);
  2. पासवर्ड फिर सेट करने के अनुरोधों को मैनेज करने के लिए, पहले verifyPasswordResetCode की मदद से ऐक्शन कोड की पुष्टि करें. इसके बाद, उपयोगकर्ता से नया पासवर्ड लें और उसे confirmPasswordReset को पास करें. उदाहरण के लिए:

    Web

    import { verifyPasswordResetCode, confirmPasswordReset } from "firebase/auth";
    
    function handleResetPassword(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
    
      // Verify the password reset code is valid.
      verifyPasswordResetCode(auth, actionCode).then((email) => {
        const accountEmail = email;
    
        // TODO: Show the reset screen with the user's email and ask the user for
        // the new password.
        const newPassword = "...";
    
        // Save the new password.
        confirmPasswordReset(auth, actionCode, newPassword).then((resp) => {
          // Password reset has been confirmed and new password updated.
    
          // TODO: Display a link back to the app, or sign-in the user directly
          // if the page belongs to the same domain as the app:
          // auth.signInWithEmailAndPassword(accountEmail, newPassword);
    
          // TODO: If a continue URL is available, display a button which on
          // click redirects the user back to the app via continueUrl with
          // additional state determined from that URL's parameters.
        }).catch((error) => {
          // Error occurred during confirmation. The code might have expired or the
          // password is too weak.
        });
      }).catch((error) => {
        // Invalid or expired action code. Ask user to try to reset the password
        // again.
      });
    }

    Web

    function handleResetPassword(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
    
      // Verify the password reset code is valid.
      auth.verifyPasswordResetCode(actionCode).then((email) => {
        var accountEmail = email;
    
        // TODO: Show the reset screen with the user's email and ask the user for
        // the new password.
        var newPassword = "...";
    
        // Save the new password.
        auth.confirmPasswordReset(actionCode, newPassword).then((resp) => {
          // Password reset has been confirmed and new password updated.
    
          // TODO: Display a link back to the app, or sign-in the user directly
          // if the page belongs to the same domain as the app:
          // auth.signInWithEmailAndPassword(accountEmail, newPassword);
    
          // TODO: If a continue URL is available, display a button which on
          // click redirects the user back to the app via continueUrl with
          // additional state determined from that URL's parameters.
        }).catch((error) => {
          // Error occurred during confirmation. The code might have expired or the
          // password is too weak.
        });
      }).catch((error) => {
        // Invalid or expired action code. Ask user to try to reset the password
        // again.
      });
    }
  3. ईमेल पता बदलने के अनुरोध को रद्द करने के लिए, पहले checkActionCode की मदद से ऐक्शन कोड की पुष्टि करें. इसके बाद, applyActionCode की मदद से उपयोगकर्ता का ईमेल पता वापस लाएं. उदाहरण के लिए:

    Web

    import { checkActionCode, applyActionCode, sendPasswordResetEmail } from "firebase/auth";
    
    function handleRecoverEmail(auth, actionCode, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      let restoredEmail = null;
      // Confirm the action code is valid.
      checkActionCode(auth, actionCode).then((info) => {
        // Get the restored email address.
        restoredEmail = info['data']['email'];
    
        // Revert to the old email.
        return applyActionCode(auth, actionCode);
      }).then(() => {
        // Account email reverted to restoredEmail
    
        // TODO: Display a confirmation message to the user.
    
        // You might also want to give the user the option to reset their password
        // in case the account was compromised:
        sendPasswordResetEmail(auth, restoredEmail).then(() => {
          // Password reset confirmation sent. Ask user to check their email.
        }).catch((error) => {
          // Error encountered while sending password reset code.
        });
      }).catch((error) => {
        // Invalid code.
      });
    }

    Web

    function handleRecoverEmail(auth, actionCode, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      var restoredEmail = null;
      // Confirm the action code is valid.
      auth.checkActionCode(actionCode).then((info) => {
        // Get the restored email address.
        restoredEmail = info['data']['email'];
    
        // Revert to the old email.
        return auth.applyActionCode(actionCode);
      }).then(() => {
        // Account email reverted to restoredEmail
    
        // TODO: Display a confirmation message to the user.
    
        // You might also want to give the user the option to reset their password
        // in case the account was compromised:
        auth.sendPasswordResetEmail(restoredEmail).then(() => {
          // Password reset confirmation sent. Ask user to check their email.
        }).catch((error) => {
          // Error encountered while sending password reset code.
        });
      }).catch((error) => {
        // Invalid code.
      });
    }
  4. ईमेल पते की पुष्टि करने के लिए, पुष्टि करने वाला बटन दिखाएं. साथ ही, applyActionCode को कॉल करने से पहले, उपयोगकर्ता से क्लिक करने के लिए कहें. उपयोगकर्ता से क्लिक कराने की शर्त रखने से, ईमेल की सुरक्षा के लिए बने ऐसे ऑटोमेटेड स्कैनर को रोका जा सकता है जो लिंक को प्रीफ़ेच कर लेते हैं. इससे, उपयोगकर्ता के पेज खोलने से पहले, पुष्टि के लिए एक बार इस्तेमाल किए जाने वाले कोड को रिडीम होने से रोका जा सकता है:

    Web

    import { applyActionCode } from "firebase/auth";
    
    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Show the confirmation button and apply the action code only after
      // the user clicks it.
      const confirmButton = document.getElementById('confirm-verify-email');
      if (!confirmButton) {
        return;
      }
      confirmButton.hidden = false;
      confirmButton.addEventListener('click', () => {
        confirmButton.disabled = true;
        applyActionCode(auth, actionCode).then(() => {
          // Email address has been verified. Display a confirmation
          // message, and if continueUrl is available, provide a button
          // that redirects back to the app.
        }).catch((error) => {
          confirmButton.disabled = false;
          // Code is invalid, expired, or failed due to a network error.
          // Ask the user to verify their email address again if needed.
        });
      });
    }
    

    Web

    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Show the confirmation button and apply the action code only after
      // the user clicks it.
      var confirmButton = document.getElementById('confirm-verify-email');
      if (!confirmButton) {
        return;
      }
      confirmButton.hidden = false;
      confirmButton.addEventListener('click', function() {
        confirmButton.disabled = true;
        auth.applyActionCode(actionCode).then(function() {
          // Email address has been verified. Display a confirmation
          // message, and if continueUrl is available, provide a button
          // that redirects back to the app.
        }).catch(function(error) {
          confirmButton.disabled = false;
          // Code is invalid, expired, or failed due to a network error.
          // Ask the user to verify their email address again if needed.
        });
      });
    }
    
  5. पेज को कहीं होस्ट करें. उदाहरण के लिए, Firebase Hosting का इस्तेमाल करें.

Firebase से पुष्टि करने की सुविधा का बैकएंड, जनरेट किए गए कार्रवाई के लिंक में एम्बेड किए गए एपीआई पासकोड को मैनेज करता है. जैसे, यूआरएल में मौजूद apiKey पैरामीटर. अगर आपके प्रोजेक्ट के एपीआई पासकोड को रोटेट किया जाता है या Firebase Auth से जुड़े ओरिजनल पासकोड को मिटाया जाता है, तो हो सकता है कि आपके उपयोगकर्ताओं को अमान्य पासकोड वाले लिंक मिलें. इससे auth/invalid-api-key गड़बड़ियां हो सकती हैं.

इस समस्या को हल करने के लिए:

  • अगर Firebase होस्टिंग का इस्तेमाल किया जा रहा है, तो: एपीआई पासकोड को init.json में कैश मेमोरी में सेव किया जा सकता है. अपनी होस्टिंग साइट (firebase deploy --only hosting) का नया वर्शन डिप्लॉय करें, ताकि बैकएंड मेटाडेटा सेवा को चालू एपीआई पासकोड के साथ कॉन्फ़िगरेशन को फिर से जनरेट करने के लिए मजबूर किया जा सके.
  • अगर बैकएंड पासकोड को ही अपडेट करना है, तो: बैकएंड एपीआई पासकोड की मैपिंग को सीधे कंसोल या एसडीके का इस्तेमाल करके नहीं बदला जा सकता. इसलिए, आपको अपने प्रोजेक्ट के लिए बैकएंड ऑथेंटिकेशन कॉन्फ़िगरेशन को अपडेट करने का अनुरोध करने के लिए, सहायता टीम से संपर्क करना होगा.

इसके बाद, आपको अपने Firebase प्रोजेक्ट को कॉन्फ़िगर करना होगा, ताकि वह उपयोगकर्ता मैनेजमेंट ईमेल में, कस्टम ईमेल ऐक्शन हैंडलर से लिंक हो सके.

कस्टम ईमेल ऐक्शन हैंडलर का इस्तेमाल करने के लिए, अपने Firebase प्रोजेक्ट को कॉन्फ़िगर करने के लिए:

  1. Firebase कंसोल में, सुरक्षा > पुष्टि > टेंप्लेट टैब पर जाएं.

  2. ईमेल टाइप की किसी भी एंट्री में, ईमेल टेंप्लेट में बदलाव करने के लिए पेंसिल आइकॉन पर क्लिक करें.

  3. कस्टम ऐक्शन यूआरएल पर क्लिक करें. इसके बाद, कस्टम ईमेल ऐक्शन हैंडलर का यूआरएल डालें.

यूआरएल सेव करने के बाद, इसका इस्तेमाल आपके Firebase प्रोजेक्ट के सभी ईमेल टेंप्लेट करेंगे.