कस्टम ईमेल एक्शन हैंडलर बनाएं

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

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

निम्नलिखित उपयोगकर्ता प्रबंधन क्रियाओं के लिए उपयोगकर्ता को ईमेल एक्शन हैंडलर का उपयोग करके कार्रवाई पूरी करने की आवश्यकता होती है:

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

अपने फायरबेस प्रोजेक्ट के ईमेल एक्शन हैंडलर को कस्टमाइज़ करने के लिए, आपको एक वेब पेज बनाना और होस्ट करना होगा जो अनुरोध की वैधता को सत्यापित करने और अनुरोध को पूरा करने के लिए फायरबेस जावास्क्रिप्ट एसडीके का उपयोग करता है। फिर, आपको अपने कस्टम एक्शन हैंडलर से लिंक करने के लिए अपने फायरबेस प्रोजेक्ट के ईमेल टेम्प्लेट को कस्टमाइज़ करना होगा।

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

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

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

    ये पैरामीटर उपयोगकर्ता प्रबंधन कार्य को निर्दिष्ट करते हैं जिसे उपयोगकर्ता पूरा कर रहा है। आपके ईमेल एक्शन हैंडलर पेज को निम्नलिखित क्वेरी पैरामीटर को संभालना होगा:

    पैरामीटर
    तरीका

    उपयोगकर्ता प्रबंधन कार्रवाई पूरी की जानी है. निम्नलिखित मानों में से एक हो सकता है:

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

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

    स्थानीयकरण को फायरबेस कंसोल के माध्यम से या ईमेल कार्रवाई को ट्रिगर करने से पहले संबंधित क्लाइंट एपीआई को कॉल करके गतिशील रूप से सेट किया जा सकता है। उदाहरण के लिए, जावास्क्रिप्ट का उपयोग करना: firebase.auth().languageCode = 'fr'; .

    लगातार उपयोगकर्ता अनुभव के लिए, सुनिश्चित करें कि ईमेल एक्शन हैंडलर स्थानीयकरण ईमेल टेम्पलेट से मेल खाता है।

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

    Web modular API

    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 namespaced API

    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 modular API

    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 namespaced API

    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 modular API

    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 namespaced API

    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 modular API

    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Try to apply the email verification code.
      applyActionCode(auth, actionCode).then((resp) => {
        // Email address has been verified.
    
        // TODO: Display a confirmation message to the user.
        // You could also provide the user with a link back to the app.
    
        // 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) => {
        // Code is invalid or expired. Ask the user to verify their email address
        // again.
      });
    }

    Web namespaced API

    function handleVerifyEmail(auth, actionCode, continueUrl, lang) {
      // Localize the UI to the selected language as determined by the lang
      // parameter.
      // Try to apply the email verification code.
      auth.applyActionCode(actionCode).then((resp) => {
        // Email address has been verified.
    
        // TODO: Display a confirmation message to the user.
        // You could also provide the user with a link back to the app.
    
        // 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) => {
        // Code is invalid or expired. Ask the user to verify their email address
        // again.
      });
    }
  5. पेज को कहीं होस्ट करें, उदाहरण के लिए फायरबेस होस्टिंग का उपयोग करें।

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

अपने कस्टम ईमेल एक्शन हैंडलर का उपयोग करने के लिए अपने फायरबेस प्रोजेक्ट को कॉन्फ़िगर करने के लिए:

  1. अपना प्रोजेक्ट फायरबेस कंसोल में खोलें।
  2. प्रामाणिक अनुभाग में ईमेल टेम्प्लेट पृष्ठ पर जाएँ।
  3. किसी भी ईमेल प्रकार प्रविष्टि में, ईमेल टेम्पलेट को संपादित करने के लिए पेंसिल आइकन पर क्लिक करें।
  4. कस्टमाइज़ एक्शन यूआरएल पर क्लिक करें, और अपने कस्टम ईमेल एक्शन हैंडलर के लिए यूआरएल निर्दिष्ट करें।

आपके द्वारा URL सहेजने के बाद, इसका उपयोग आपके सभी फायरबेस प्रोजेक्ट के ईमेल टेम्प्लेट द्वारा किया जाएगा।