کنترل کننده های اقدام ایمیل سفارشی ایجاد کنید

برخی از اقدامات مدیریت کاربر، مانند به روز رسانی آدرس ایمیل کاربر و تنظیم مجدد رمز عبور کاربر، منجر به ارسال ایمیل برای کاربر می شود. این ایمیل‌ها حاوی پیوندهایی هستند که گیرندگان می‌توانند آن‌ها را برای تکمیل یا لغو عملکرد مدیریت کاربر باز کنند. به طور پیش‌فرض، ایمیل‌های مدیریت کاربر به کنترل‌کننده اقدام پیش‌فرض پیوند دارند، که یک صفحه وب است که در یک URL در دامنه میزبانی Firebase پروژه شما میزبانی می‌شود.

در عوض می‌توانید یک کنترل‌کننده اقدام ایمیل سفارشی برای انجام پردازش سفارشی و ادغام کنترل‌کننده اقدام ایمیل با وب‌سایت خود ایجاد و میزبانی کنید.

اقدامات مدیریت کاربر زیر، کاربر را ملزم می‌کند تا با استفاده از کنترل‌کننده اقدام ایمیل، اقدام را تکمیل کند:

  • بازنشانی رمزهای عبور
  • لغو تغییرات آدرس ایمیل—زمانی که کاربران آدرس های ایمیل اصلی حساب های خود را تغییر می دهند، 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 یک کد یکبار مصرف که برای شناسایی و تأیید یک درخواست استفاده می شود
    کلید ای پی ای کلید API پروژه Firebase شما که برای سهولت ارائه شده است
    continueUrl این یک URL اختیاری است که راهی برای بازگرداندن وضعیت به برنامه از طریق URL ارائه می دهد. این مربوط به حالت های بازنشانی رمز عبور و تأیید ایمیل است. هنگام ارسال ایمیل بازنشانی رمز عبور یا ایمیل تأیید، یک شی ActionCodeSettings باید با URL ادامه پیدا کند تا در دسترس باشد. این امکان را برای کاربر فراهم می‌کند که پس از یک اقدام ایمیل، درست از همان جایی که کار را متوقف کرده است، ادامه دهد.
    زبان

    این تگ زبان اختیاری BCP47 است که موقعیت کاربر را نشان می دهد (به عنوان مثال، fr ). می توانید از این مقدار برای ارائه صفحات کنترل کننده اقدام ایمیل محلی به کاربران خود استفاده کنید.

    محلی سازی را می توان از طریق کنسول Firebase یا به صورت پویا با فراخوانی API مشتری مربوطه قبل از شروع عمل ایمیل تنظیم کرد. به عنوان مثال، با استفاده از جاوا اسکریپت: 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': "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 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 استفاده کنید.

در مرحله بعد، باید پروژه Firebase خود را به گونه‌ای پیکربندی کنید که در ایمیل‌های مدیریت کاربر به کنترل‌کننده اقدام ایمیل سفارشی شما پیوند داده شود.

برای پیکربندی پروژه Firebase برای استفاده از کنترل کننده اقدام ایمیل سفارشی:

  1. پروژه خود را در کنسول Firebase باز کنید.
  2. به صفحه Email Templates در بخش Auth بروید.
  3. در هر یک از ورودی‌های انواع ایمیل ، روی نماد مداد کلیک کنید تا الگوی ایمیل را ویرایش کنید.
  4. روی سفارشی کردن اقدام URL کلیک کنید و URL را برای کنترل کننده اقدام ایمیل سفارشی خود مشخص کنید.

پس از اینکه URL را ذخیره کردید، توسط تمام قالب های ایمیل پروژه Firebase شما استفاده می شود.