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

उपयोगकर्ता मैनेजमेंट से जुड़ी कुछ कार्रवाइयों की वजह से, उपयोगकर्ता को ईमेल भेजे जाते हैं. जैसे, उपयोगकर्ता का ईमेल पता अपडेट करना और उसका पासवर्ड रीसेट करना. इन ईमेल में लिंक होते हैं. उपयोगकर्ता मैनेजमेंट ऐक्शन को पूरा करने या रद्द करने के लिए, ईमेल पाने वाले लोग ये लिंक खोल सकते हैं. डिफ़ॉल्ट रूप से, उपयोगकर्ता मैनेजमेंट ईमेल, डिफ़ॉल्ट ऐक्शन हैंडलर से लिंक होते हैं. यह आपके प्रोजेक्ट के 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

    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

    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 का इस्तेमाल करें.

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

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

  1. Firebase कंसोल में अपना प्रोजेक्ट खोलें.
  2. पुष्टि सेक्शन में, ईमेल टेंप्लेट पेज पर जाएं.
  3. ईमेल टाइप की किसी भी एंट्री में, ईमेल टेंप्लेट में बदलाव करने के लिए, पेंसिल आइकॉन पर क्लिक करें.
  4. कार्रवाई के यूआरएल को पसंद के मुताबिक बनाएं पर क्लिक करें. इसके बाद, अपने कस्टम ईमेल ऐक्शन हैंडलर का यूआरएल डालें.

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