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

ব্যবহারকারী ম্যানেজমেন্ট সংক্রান্ত কিছু অ্যাকশন, যেমন ব্যবহারকারীর ইমেল আইডি আপডেট করা এবং ব্যবহারকারীর পাসওয়ার্ড রিসেট করার ফলে ব্যবহারকারীকে ইমেল পাঠানো হয়। এইসব ইমেলে এমন লিঙ্ক থাকে যা প্রাপক খুলে ব্যবহারকারী ম্যানেজমেন্ট অ্যাকশন সম্পূর্ণ বা বাতিল করতে পারেন। সাধারণত, ব্যবহারকারী ম্যানেজমেন্ট ইমেল ডিফল্ট অ্যাকশন হ্যান্ডলারের সাথে লিঙ্ক করে, যা আপনার প্রোজেক্টের Firebase হোস্টিং ডোমেনের 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 key, সুবিধার জন্য প্রদান করা হয়েছে
    continueUrl এটি একটি ঐচ্ছিক URL যা URL-এর মাধ্যমে অ্যাপে স্ট্যাটাস ফেরত পাঠানোর সুযোগ দেয়। এটি পাসওয়ার্ড রিসেট ও ইমেল যাচাইকরণ মোডের ক্ষেত্রে প্রাসঙ্গিক। পাসওয়ার্ড রিসেট করার ইমেল বা যাচাইকরণ ইমেল পাঠানোর সময়, এটি উপলভ্য করার জন্য এম্বেড করা ইউআরএল সহ একটি ActionCodeSettings অবজেক্ট নির্দিষ্ট করতে হবে। এর ফলে ব্যবহারকারী ইমেল অ্যাকশন নেওয়ার পরে ঠিক যেখান থেকে ছেড়েছিলেন সেখান থেকেই আবার শুরু করতে পারেন।
    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

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

এই সমস্যার সমাধান করতে:

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

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

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

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

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

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

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