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

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

    นี่คือแท็กภาษา 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

    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 จะจัดการคีย์ API ที่ฝังอยู่ในลิงก์การดำเนินการที่สร้างขึ้น เช่น พารามิเตอร์ apiKey ใน URL หากคุณหมุนเวียนคีย์ API ของโปรเจ็กต์หรือลบคีย์ที่เชื่อมโยงกับ Firebase Auth ตั้งแต่แรก ผู้ใช้อาจได้รับลิงก์ที่มีคีย์ไม่ถูกต้อง ซึ่งส่งผลให้เกิดข้อผิดพลาด auth/invalid-api-key

วิธีแก้ไขปัญหามีดังนี้

  • หากใช้โฮสติ้งของ Firebase: ระบบอาจแคชคีย์ API ไว้ใน init.json ทำให้เว็บไซต์โฮสติ้งเวอร์ชันใหม่ (firebase deploy --only hosting) ใช้งานได้เพื่อบังคับให้บริการข้อมูลเมตาแบ็กเอนด์สร้างการกำหนดค่าใหม่ด้วยคีย์ API ที่ใช้งานอยู่
  • หากจำเป็นต้องอัปเดตคีย์แบ็กเอนด์: เนื่องจากไม่สามารถแก้ไขการแมปคีย์ API แบ็กเอนด์ได้โดยตรงโดยใช้คอนโซลหรือ SDK คุณจึงต้องติดต่อทีมสนับสนุนเพื่อขออัปเดตการกำหนดค่าการตรวจสอบสิทธิ์แบ็กเอนด์สำหรับโปรเจ็กต์

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

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

  1. ในคอนโซล Firebase ให้ไปที่แท็บความปลอดภัย > การตรวจสอบสิทธิ์ > เทมเพลต

  2. ในรายการประเภทอีเมล ให้คลิกไอคอนดินสอเพื่อแก้ไขเทมเพลตอีเมล

  3. คลิกปรับแต่ง URL การกระทำที่กำหนดเอง แล้วระบุ URL ไปยังเครื่องจัดการการกระทำที่กำหนดเองทางอีเมล

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