您可以觸發函式,以回應 Firebase Authentication 使用者帳戶的建立和刪除。比方說,您可以向剛在您的應用程式中建立帳戶的使用者傳送歡迎電子郵件。本頁範例是根據一個範例,該範例在建立和刪除帳戶時傳送歡迎和告別電子郵件。
如需更多用途範例,請參閱「Cloud Functions 的用途」。
在建立使用者時觸發函式
您可以使用 firebase-functions/v2/identity 子套件中的 onUserCreated 事件處理常式,建立在建立 Authentication 使用者時觸發的函式:
const { onUserCreated } = require("firebase-functions/identity"); const { defineSecret } = require("firebase-functions/params"); const { logger } = require("firebase-functions"); const { sendWelcomeEmail } = require("./utils/myEmailService"); const emailApiKey = defineSecret("EMAIL_API_KEY"); exports.newUserWelcome = onUserCreated( { secrets: [emailApiKey] }, async (event) => { const { uid, email, displayName } = event.data; if (!email) { logger.log(`User ${uid} does not have an email address.`); return; } await sendWelcomeEmail(email, displayName); }, );
Authentication 帳戶會在下列情況,針對 Cloud Functions 觸發使用者建立事件:
- 使用者建立電子郵件帳戶與密碼。
- 使用者首次透過聯合身分識別資訊提供者登入。
- 開發人員使用 Admin SDK 建立帳戶。
- 使用者第一次登入新匿名驗證工作階段。
使用者首次使用自訂權杖登入時,不會觸發 Cloud Functions 事件。
設定觸發條件選項和多用戶群
您可以將選項物件 (AuthOptions) 做為第一個參數傳遞至 onUserCreated,藉此設定函式:
/** * Sends a welcome email scoped to a specific tenant in Identity Platform. */ exports.sendWelcomeEmailToTenant = onUserCreated( { secrets: [emailApiKey], // Only trigger when a user is a member of this tenant tenantId: "my-tenant-id", }, async (event) => { const { uid, email, displayName } = event.data; // Customize the email for this tenant await sendWelcomeEmail(email, displayName, event.tenantId); }, ); /** * Sends a welcome email only to users not associated with any tenant. */ exports.sendWelcomeEmailNoTenant = onUserCreated( { secrets: [emailApiKey], // Only trigger when a user is NOT a member of a tenant tenantId: IS_NOT_TENANT, }, async (event) => { const { email, displayName } = event.data; // Send a generic welcome email await sendWelcomeEmail(email, displayName); }, );
如果專案使用 Identity Platform 多租戶架構,您可以設定觸發條件的範圍:
- 預設專案 (無租戶):將
tenantId設為IS_NOT_TENANT,只監聽在預設專案中建立的使用者。 - 特定租戶:提供租戶的字串 ID (例如
{ tenantId: "tenant-id-1" }),只監聽在該租戶中建立的使用者。 - 所有租戶和使用者:如果省略
tenantId,函式會在專案中所有租戶和預設專案使用者的使用者建立事件上觸發。
除了 tenantId 之外,您還可以指定標準第 2 代設定選項,包括 region、concurrency、cpu、memory、timeoutSeconds、minInstances、maxInstances 和 secrets。
存取使用者屬性
從傳回至函式的使用者資料中,您可以透過 event.data 存取新建立使用者 UserRecord 物件中的使用者屬性清單。舉例來說,您可以取得使用者的電子郵件和顯示名稱,如下所示:
const { uid, email, displayName } = event.data;
第 2 代的驗證觸發條件會接收 AuthEvent 物件。除了 event.data 之外,您還可以存取事件中繼資料,例如:
event.id:事件的專屬 ID。event.type:事件類型 (google.firebase.auth.user.v2.created)。event.time:ISO 8601 時間戳記,代表事件發生的時間。event.project:Google Cloud 專案 ID。event.tenantId:與使用者相關聯的 Identity Platform 租用戶 ID (如適用)。
在使用者刪除帳戶時觸發函式
就像在建立使用者時觸發函式一樣,您也可以回應使用者刪除事件。請使用 firebase-functions/v2/identity 的 onUserDeleted 事件處理常式,如下所示:
const { onUserDeleted } = require("firebase-functions/identity"); const { defineSecret } = require("firebase-functions/params"); const { logger } = require("firebase-functions"); const { sendGoodbyeEmail } = require("./utils/myEmailService"); const emailApiKey = defineSecret("EMAIL_API_KEY"); exports.deletedUserFarewell = onUserDeleted( { secrets: [emailApiKey] }, async (event) => { const { uid, email, displayName } = event.data; if (!email) { logger.log(`User ${uid} does not have an email address.`); return; } await sendGoodbyeEmail(email, displayName); }, );
與 onUserCreated 相同,您可以使用 { tenantId: IS_NOT_TENANT } 等選項設定 onUserDeleted,將觸發條件限制為預設專案中的使用者。
觸發封鎖函式
如果您已升級至 Firebase Authentication with Identity Platform,可以使用封鎖函式擴充 Firebase Authentication。
封鎖函式可讓您同步執行自訂程式碼,藉此修改使用者註冊或登入應用程式的結果。背景觸發條件會在事件完成後以非同步方式執行,而封鎖函式則可讓您在使用者不符合特定條件時,禁止他們進行驗證,或是在將使用者資訊和宣告傳回用戶端應用程式之前,先更新這些資訊和宣告。
第 2 代觸發條件的最佳做法
實作第 2 代驗證觸發條件時,請留意下列最佳做法:
- 考量並行處理:Cloud Functions (第 2 代) 執行個體會處理並行要求 (CPU ≥ 1 時,預設為 80 個並行要求)。請確保函式不會在並行執行作業之間,依賴全域可變動狀態。
- 設計冪等性:第 2 代事件傳遞會透過 Eventarc 至少執行一次。請確認函式具有冪等性,例如在執行副作用前,確認尚未傳送歡迎電子郵件或初始化資料庫項目。
- 設定多租戶函式的範圍:如果應用程式使用 Identity Platform 多租戶功能,請確認函式應處理所有租戶的事件,還是僅處理特定租戶的事件。使用
tenantId: IS_NOT_TENANT,防止租戶使用者觸發僅限主要專案使用的函式。 - 管理區域和資源分配:指定函式位置 (
region),盡可能縮短驗證服務供應商與函式執行環境之間的網路延遲時間。