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

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

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

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

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

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

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

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

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

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

    প্যারামিটার
    মোড

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

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

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

    ফায়ারবেস কনসোলের মাধ্যমে অথবা ইমেল অ্যাকশনটি ট্রিগার করার আগে সংশ্লিষ্ট ক্লায়েন্ট এপিআই কল করে ডায়নামিকভাবে স্থানীয়করণ সেট করা যেতে পারে। উদাহরণস্বরূপ, জাভাস্ক্রিপ্ট ব্যবহার করে: 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 ব্যবহার করুন।

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

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

  1. Firebase কনসোলে, Security > Authentication > Templates ট্যাবে যান।

  2. যেকোনো 'ইমেল টাইপস' এন্ট্রিতে, ইমেল টেমপ্লেটটি সম্পাদনা করতে পেন্সিল আইকনে ক্লিক করুন।

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

ইউআরএলটি সংরক্ষণ করার পর, এটি আপনার ফায়ারবেস প্রজেক্টের সমস্ত ইমেল টেমপ্লেটে ব্যবহৃত হবে।