কাস্টম ইমেল অ্যাকশন হ্যান্ডলার তৈরি করা

ব্যবহারকারীর ইমেল আইডি আপডেট করা এবং ব্যবহারকারীর পাসওয়ার্ড রিসেট করার মতো কিছু ব্যবহারকারী ম্যানেজমেন্ট অ্যাকশনের ফলে ব্যবহারকারীকে ইমেল পাঠানো হয়। এইসব ইমেলে এমন লিঙ্ক থাকে যা প্রাপকরা ব্যবহারকারী ম্যানেজমেন্ট অ্যাকশন সম্পূর্ণ বা বাতিল করতে খুলতে পারেন। সাধারণত, ব্যবহারকারী ম্যানেজমেন্ট ইমেল ডিফল্ট অ্যাকশন হ্যান্ডলারের সাথে লিঙ্ক করে, এটি আপনার প্রোজেক্টের Firebase Hosting ডোমেনে URL-এ হোস্ট করা একটি ওয়েব পেজ।

এর পরিবর্তে, কাস্টম প্রসেসিং করতে এবং আপনার ওয়েবসাইটের সাথে ইমেল অ্যাকশন হ্যান্ডলার ইন্টিগ্রেট করতে, আপনি একটি কাস্টম ইমেল অ্যাকশন হ্যান্ডলার তৈরি ও হোস্ট করতে পারেন।

ব্যবহারকারী ম্যানেজমেন্ট সংক্রান্ত নিম্নলিখিত অ্যাকশনগুলি সম্পূর্ণ করার জন্য ব্যবহারকারীকে ইমেল অ্যাকশন হ্যান্ডলার ব্যবহার করতে হবে:

  • পাসওয়ার্ড রিসেট করা
  • ইমেল আইডি পরিবর্তন করার সুবিধা প্রত্যাহার করা—ব্যবহারকারীরা তাদের অ্যাকাউন্টের প্রাথমিক ইমেল আইডি পরিবর্তন করলে, Firebase তাদের পুরনো ইমেল আইডিতে একটি ইমেল পাঠায় যা তাদের পরিবর্তনটি আগের অবস্থায় ফিরিয়ে আনতে সাহায্য করে
  • ইমেল আইডি যাচাই করা

আপনার Firebase প্রোজেক্টের ইমেল অ্যাকশন হ্যান্ডলার কাস্টমাইজ করতে, আপনাকে অবশ্যই একটি ওয়েব পেজ তৈরি ও হোস্ট করতে হবে যা Firebase JavaScript SDK ব্যবহার করে অনুরোধের বৈধতা যাচাই করে এবং অনুরোধ সম্পূর্ণ করে। তারপরে, আপনার কাস্টম অ্যাকশন হ্যান্ডলারের সাথে লিঙ্ক করতে, আপনাকে Firebase প্রজেক্টের ইমেল টেমপ্লেট কাস্টমাইজ করতে হবে।

ইমেল অ্যাকশন হ্যান্ডলার পৃষ্ঠা তৈরি করা

  1. Firebase, ব্যবহারকারী ম্যানেজমেন্ট ইমেল তৈরি করার সময় আপনার অ্যাকশন হ্যান্ডলার URL-এ বেশ কিছু কোয়েরি প্যারামিটার যোগ করে। যেমন:

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

    এইসব প্যারামিটার ব্যবহারকারী ম্যানেজমেন্ট টাস্ক নির্দিষ্ট করে যা ব্যবহারকারী সম্পূর্ণ করছেন। আপনার ইমেল অ্যাকশন হ্যান্ডলার পৃষ্ঠাকে নিম্নলিখিত কোয়েরি প্যারামিটারগুলি অবশ্যই হ্যান্ডেল করতে হবে:

    প্যারামিটারগুলি
    মোড

    ব্যবহারকারী ম্যানেজমেন্ট অ্যাকশন সম্পূর্ণ করতে হবে। নিম্নলিখিত মানগুলির মধ্যে যেকোনও একটি হতে পারে:

    • resetPassword
    • recoverEmail
    • verifyEmail
    oobCode অনুরোধ শনাক্ত ও যাচাই করার জন্য ব্যবহৃত এককালীন কোড
    apiKey আপনার Firebase প্রোজেক্টের API কী, সুবিধার জন্য প্রদান করা হয়েছে
    continueUrl এটি একটি ঐচ্ছিক URL যা URL-এর মাধ্যমে অ্যাপে স্টেট পাস করার একটি উপায় প্রদান করে। এটি পাসওয়ার্ড রিসেট ও ইমেল যাচাইকরণ মোডের ক্ষেত্রে প্রযোজ্য। পাসওয়ার্ড রিসেট করার ইমেল বা যাচাইকরণ ইমেল পাঠানোর সময়, এর উপলভ্যতা নিশ্চিত করতে ActionCodeSettings অবজেক্টের সাথে চালিয়ে যাওয়ার URL নির্দিষ্ট করতে হবে। এর ফলে ইমেল অ্যাকশনের পরে ব্যবহারকারী ঠিক যেখান থেকে শেষ করেছিলেন সেখান থেকেই আবার শুরু করতে পারেন।
    lang

    এটি হল ঐচ্ছিক BCP47 ল্যাঙ্গুয়েজ ট্যাগ যা ব্যবহারকারীর লোকেল (যেমন, fr) বোঝায়। আপনার ব্যবহারকারীদের লোকালাইজ করা ইমেল অ্যাকশন হ্যান্ডলার পৃষ্ঠা প্রদান করতে আপনি এই ভ্যালু ব্যবহার করতে পারেন।

    Firebase কনসোলের মাধ্যমে অথবা ইমেল অ্যাকশন ট্রিগার করার আগে সংশ্লিষ্ট ক্লায়েন্ট API কল করে ডায়নামিক উপায়ে লোকালাইজেশন সেট করা যেতে পারে। যেমন, জাভাস্ক্রিপ্ট ব্যবহার করে: 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 Authentication ব্যাকএন্ড, জেনারেট করা অ্যাকশন লিঙ্কে এম্বেড করা API কী ম্যানেজ করে, যেমন URL-এ apiKey প্যারামিটার। আপনি যদি নিজের প্রোজেক্টের API কী রোটেট করেন অথবা Firebase Auth-এর সাথে আসলভাবে যুক্ত থাকা কীটি মুছে দেন, তাহলে আপনার ব্যবহারকারীরা ভুল কী সহ লিঙ্ক পেতে পারেন, যার ফলে auth/invalid-api-key সমস্যা হতে পারে।

এর সমাধান করতে:

  • আপনি Firebase Hosting ব্যবহার করলে: API কী init.json-এ ক্যাশে করা থাকতে পারে। অ্যাক্টিভ API কী সহ কনফিগারেশন আবার তৈরি করতে বাধ্য করার জন্য আপনার হোস্টিং সাইটের (firebase deploy --only hosting) একটি নতুন ভার্সন ডেপ্লয় করুন।
  • ব্যাকএন্ড কী আপডেট করার প্রয়োজন হলে: কনসোল বা SDK ব্যবহার করে ব্যাকএন্ড API কী ম্যাপিং সরাসরি পরিবর্তন করা যায় না, তাই আপনার প্রজেক্টের জন্য ব্যাকএন্ড যাচাইকরণ কনফিগারেশন আপডেট করার অনুরোধ জানাতে আপনাকে অবশ্যই সহায়তা টিমের সাথে যোগাযোগ করতে হবে।

এরপরে, ব্যবহারকারী ম্যানেজমেন্ট ইমেলে আপনার কাস্টম ইমেল অ্যাকশন হ্যান্ডলারের সাথে লিঙ্ক করার জন্য Firebase প্রোজেক্ট কনফিগার করতে হবে।

আপনার কাস্টম ইমেল অ্যাকশন হ্যান্ডলার ব্যবহার করার জন্য Firebase প্রোজেক্ট কনফিগার করতে:

  1. Firebase কনসোলে গিয়ে নিরাপত্তা > যাচাইকরণ > টেমপ্লেট ট্যাব দেখুন।

  2. যেকোনও ইমেলের ধরন এন্ট্রি থেকে, ইমেল টেমপ্লেট এডিট করতে পেন্সিল আইকনে ক্লিক করুন।

  3. অ্যাকশন URL কাস্টমাইজ করুন বিকল্পে ক্লিক করুন এবং আপনার কাস্টম ইমেল অ্যাকশন হ্যান্ডলারের URL নির্দিষ্ট করুন।

URL সেভ করার পরে, এটি আপনার Firebase প্রকল্পের সমস্ত ইমেল টেমপ্লেট ব্যবহার করবে।