สร้างเครื่องจัดการอีเมลที่กำหนดเอง

การดำเนินการบางอย่างเกี่ยวกับการจัดการผู้ใช้ เช่น การอัปเดตอีเมลของผู้ใช้และ การรีเซ็ตรหัสผ่านของผู้ใช้จะส่งผลให้ระบบส่งอีเมลไปยังผู้ใช้ เหล่านี้ อีเมลมีลิงก์ที่ผู้รับสามารถเปิดเพื่อดำเนินการให้เสร็จสมบูรณ์หรือยกเลิกผู้ใช้ การจัดการ โดยค่าเริ่มต้น อีเมลการจัดการผู้ใช้จะลิงก์ไปยังการดำเนินการเริ่มต้น เครื่องจัดการ ซึ่งเป็นหน้าเว็บที่โฮสต์ที่ 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
    รหัส oob รหัสแบบใช้ครั้งเดียวซึ่งใช้เพื่อระบุและยืนยันคำขอ
    คีย์ API คีย์ API ของโปรเจ็กต์ Firebase มีไว้เพื่ออำนวยความสะดวก
    URL ดำเนินการต่อ นี่คือ URL ที่ไม่บังคับซึ่งจะระบุวิธีส่งสถานะกลับไปยัง แอปผ่าน URL ส่วนนี้เกี่ยวข้องกับการรีเซ็ตรหัสผ่านและอีเมล โหมดการยืนยัน เมื่อส่งอีเมลรีเซ็ตรหัสผ่านหรือ อีเมลยืนยัน ออบเจ็กต์ ActionCodeSettings รายการต้องมี ด้วย URL ที่ดำเนินการต่อเพื่อให้พร้อมใช้งาน ช่วงเวลานี้ ทำให้ผู้ใช้สามารถทำงานต่อจากที่ค้างไว้ได้ หลังการดำเนินการกับอีเมล
    ภาษา

    ตัวเลือกนี้เป็นตัวเลือก BCP47 แท็กภาษาที่แสดงสถานที่ของผู้ใช้ (เช่น fr). คุณใช้ค่านี้เพื่อระบุอีเมลที่แปลเป็นภาษาท้องถิ่นได้ หน้าเครื่องจัดการการดำเนินการให้กับผู้ใช้ของคุณ

    คุณสามารถตั้งค่าการแปลผ่านคอนโซล Firebase หรือแบบไดนามิกได้โดย เรียกใช้ API ของไคลเอ็นต์ที่เกี่ยวข้องก่อนทริกเกอร์อีเมล การดำเนินการ ตัวอย่างเช่น การใช้ 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

ถัดไป คุณต้องกำหนดค่าโปรเจ็กต์ Firebase ให้ลิงก์กับอีเมลที่กำหนดเอง เครื่องจัดการการดำเนินการในอีเมลการจัดการผู้ใช้

ในการกำหนดค่าโปรเจ็กต์ Firebase ให้ใช้เครื่องจัดการอีเมลที่กำหนดเอง

  1. เปิดโปรเจ็กต์ในคอนโซล Firebase
  2. ไปที่หน้าเทมเพลตอีเมลในส่วนการตรวจสอบสิทธิ์
  3. ในรายการประเภทอีเมล ให้คลิกไอคอนดินสอเพื่อแก้ไข เทมเพลตอีเมล
  4. คลิกปรับแต่ง URL การดำเนินการ แล้วระบุ URL ไปยังอีเมลที่กำหนดเอง เครื่องจัดการการดำเนินการ

หลังจากบันทึก URL แล้ว ระบบจะใช้ URL นั้นในอีเมลของโปรเจ็กต์ Firebase ทั้งหมด เทมเพลต