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

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

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

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

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

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

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

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

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

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

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

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

    • resetPassword
    • recoverEmail
    • verifyEmail
    oobCode एक बार का कोड, जिसका उपयोग किसी अनुरोध को पहचानने और सत्यापित करने के लिए किया जाता है
    एपीआई कुंजी आपके Firebase प्रोजेक्ट की API कुंजी, सुविधा के लिए प्रदान की गई
    जारी रखें यह एक वैकल्पिक यूआरएल है जो यूआरएल के माध्यम से राज्य को ऐप में वापस भेजने का एक तरीका प्रदान करता है। यह पासवर्ड रीसेट और ईमेल सत्यापन मोड के लिए प्रासंगिक है। पासवर्ड रीसेट ईमेल या सत्यापन ईमेल भेजते समय, एक 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': "YOU_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. पृष्ठ को कहीं होस्ट करें, उदाहरण के लिए Firebase Hosting का उपयोग करें।

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

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

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

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