移動應用程序有時需要與用戶交互,並通過發送電子郵件提示他們採取某些行動。
Firebase客戶端 SDK能夠向用戶發送包含鏈接的電子郵件,這些鏈接可用於密碼重置、電子郵件地址驗證和基於電子郵件的登錄。這些基於模板的電子郵件由 Google 發送,可定制性有限。
如果您想改用您自己的電子郵件模板和您自己的電子郵件遞送服務,本頁面將介紹如何使用Firebase Admin SDK以編程方式為上述流程生成操作鏈接,您可以將其包含在發送給用戶的電子郵件中。
這具有以下好處:
- 自定義電子郵件模板。這包括添加新樣式和自定義品牌、更改措辭和徽標、使用名字而不是全名來稱呼用戶等等的能力。
- 根據上下文應用不同的模板。例如,如果用戶正在驗證他們的電子郵件以訂閱時事通訊,則可能需要在電子郵件內容中提供上下文。另一個示例是電子郵件鏈接登錄:在一種情況下,這可能由同一用戶觸發,或者作為另一個用戶的邀請觸發。上下文需要包含在電子郵件中。
- 本地化自定義的電子郵件模板。
- 能夠從安全的服務器環境生成鏈接。
- 能夠通過移動應用程序或瀏覽器自定義鏈接的打開方式,以及如何傳遞額外的狀態信息等。
- 能夠在構建電子郵件操作鏈接時自定義用於移動應用程序流的動態鏈接域,甚至可以根據上下文或移動應用程序指定不同的動態鏈接域。
初始化動作代碼設置
在生成電子郵件操作鏈接之前,您可能需要初始化一個ActionCodeSettings
實例。
ActionCodeSettings
允許您通過用戶單擊電子郵件鏈接後可訪問的繼續 URL 傳遞其他狀態。這也使用戶能夠在操作完成後返回應用程序。此外,您可以指定是在安裝移動應用程序時直接處理電子郵件操作鏈接,還是從瀏覽器處理電子郵件操作鏈接。
對於打算通過移動應用打開的鏈接,您需要啟用 Firebase 動態鏈接並執行一些任務以從您的移動應用中檢測這些鏈接。請參閱有關如何為電子郵件操作配置 Firebase 動態鏈接的說明。
要初始化ActionCodeSettings
實例,請提供以下數據:
範圍 | 類型 | 描述 |
---|---|---|
url | 細繩 | 設置在不同上下文中具有不同含義的鏈接(狀態/繼續 URL):
|
iOS | ({bundleId:字符串}|未定義) | 設置捆綁 ID。如果已安裝,這將嘗試在 Apple 應用程序中打開鏈接。該應用程序需要在控制台中註冊。 |
android | ({packageName: string, installApp:boolean|undefined, minimumVersion: string|undefined}|undefined) | 設置 Android 包名稱。如果已安裝,這將嘗試在 Android 應用程序中打開該鏈接。如果installApp 被傳遞,它指定如果設備支持並且尚未安裝該應用程序,是否安裝 Android 應用程序。如果此字段未提供packageName ,則會引發錯誤,說明packageName 必須與此字段一起提供。如果指定了minimumVersion ,並且安裝了舊版本的應用程序,用戶將被帶到 Play 商店以升級應用程序。 Android 應用需要在控制台中註冊。 |
handleCodeInApp | (布爾值|未定義) | 電子郵件操作鏈接是首先在移動應用程序中打開還是在 Web 鏈接中打開。默認值為假。設置為 true 時,操作代碼鏈接將作為通用鏈接或 Android 應用鏈接發送,如果已安裝,將由應用打開。在 false 情況下,代碼將首先發送到 Web 小部件,然後繼續將重定向到已安裝的應用程序。 |
dynamicLinkDomain | (字符串|未定義) | 如果要使用 Firebase 動態鏈接打開當前鏈接,則設置用於當前鏈接的動態鏈接域(或子域)。由於每個項目可以配置多個動態鏈接域,因此該字段提供了明確選擇一個的能力。如果未提供,則默認使用最舊的域。 |
以下示例說明瞭如何發送將首先在移動應用中打開的電子郵件驗證鏈接作為 Firebase 動態鏈接(Apple 應用com.example.ios
或 Android 應用com.example.android
,如果尚未安裝該應用將在其中安裝,並且最低版本為 12)。深層鏈接將包含繼續 URL 有效負載https://www.example.com/checkout?cartId=1234
。使用的動態鏈接域是coolapp.page.link
,必須配置為與 Firebase 動態鏈接一起使用。
節點.js
const actionCodeSettings = {
// URL you want to redirect back to. The domain (www.example.com) for
// this URL must be whitelisted in the Firebase Console.
url: 'https://www.example.com/checkout?cartId=1234',
// This must be true for email link sign-in.
handleCodeInApp: true,
iOS: {
bundleId: 'com.example.ios',
},
android: {
packageName: 'com.example.android',
installApp: true,
minimumVersion: '12',
},
// FDL custom domain.
dynamicLinkDomain: 'coolapp.page.link',
};
爪哇
ActionCodeSettings actionCodeSettings = ActionCodeSettings.builder()
.setUrl("https://www.example.com/checkout?cartId=1234")
.setHandleCodeInApp(true)
.setIosBundleId("com.example.ios")
.setAndroidPackageName("com.example.android")
.setAndroidInstallApp(true)
.setAndroidMinimumVersion("12")
.setDynamicLinkDomain("coolapp.page.link")
.build();
Python
action_code_settings = auth.ActionCodeSettings(
url='https://www.example.com/checkout?cartId=1234',
handle_code_in_app=True,
ios_bundle_id='com.example.ios',
android_package_name='com.example.android',
android_install_app=True,
android_minimum_version='12',
dynamic_link_domain='coolapp.page.link',
)
去
actionCodeSettings := &auth.ActionCodeSettings{
URL: "https://www.example.com/checkout?cartId=1234",
HandleCodeInApp: true,
IOSBundleID: "com.example.ios",
AndroidPackageName: "com.example.android",
AndroidInstallApp: true,
AndroidMinimumVersion: "12",
DynamicLinkDomain: "coolapp.page.link",
}
C#
var actionCodeSettings = new ActionCodeSettings()
{
Url = "https://www.example.com/checkout?cartId=1234",
HandleCodeInApp = true,
IosBundleId = "com.example.ios",
AndroidPackageName = "com.example.android",
AndroidInstallApp = true,
AndroidMinimumVersion = "12",
DynamicLinkDomain = "coolapp.page.link",
};
要了解更多信息,請參閱在電子郵件操作中傳遞狀態。
生成密碼重置電子郵件鏈接
要生成密碼重置鏈接,請提供現有用戶的電子郵件和可選的ActionCodeSettings
對象。該操作將通過電子郵件操作鏈接解決。使用的電子郵件必須屬於現有用戶。
節點.js
// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
.generatePasswordResetLink(userEmail, actionCodeSettings)
.then((link) => {
// Construct password reset email template, embed the link and send
// using custom SMTP server.
return sendCustomPasswordResetEmail(userEmail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
爪哇
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generatePasswordResetLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
Python
email = 'user@example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
去
email := "user@example.com"
link, err := client.PasswordResetLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct password reset template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
C#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
生成鏈接後,可以將其插入自定義密碼重置電子郵件中,然後使用自定義 SMTP 服務器通過電子郵件發送給相應的用戶。
如果您沒有使用默認密碼重置登錄頁面並構建您自己的自定義處理程序,請參閱創建自定義電子郵件操作處理程序。
生成電子郵件驗證鏈接
要生成電子郵件驗證鏈接,請提供現有用戶的未驗證電子郵件和可選的ActionCodeSettings
對象。該操作將通過電子郵件操作鏈接解決。使用的電子郵件必須屬於現有用戶。
節點.js
// Admin SDK API to generate the email verification link.
const useremail = 'user@example.com';
getAuth()
.generateEmailVerificationLink(useremail, actionCodeSettings)
.then((link) => {
// Construct email verification template, embed the link and send
// using custom SMTP server.
return sendCustomVerificationEmail(useremail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
爪哇
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generateEmailVerificationLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
Python
email = 'user@example.com'
link = auth.generate_email_verification_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
去
email := "user@example.com"
link, err := client.EmailVerificationLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
C#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
生成鏈接後,可以將其插入到自定義驗證電子郵件中,然後使用自定義 SMTP 服務器通過電子郵件發送給相應的用戶。
如果您沒有使用默認的電子郵件驗證登錄頁面並構建您自己的自定義處理程序,請參閱創建自定義電子郵件操作處理程序。
生成用於登錄的電子郵件鏈接
在您可以通過電子郵件鏈接登錄對用戶進行身份驗證之前,您需要為您的 Firebase 項目啟用電子郵件鏈接登錄。
要生成登錄鏈接,請提供用戶的電子郵件和ActionCodeSettings
對象。在這種情況下,需要ActionCodeSettings
對象來提供有關在單擊鏈接以完成登錄後返回用戶的位置的信息。該操作將通過電子郵件操作鏈接解決。
與密碼重置和電子郵件驗證不同,使用的電子郵件不一定需要屬於現有用戶,因為此操作可用於通過電子郵件鏈接將新用戶註冊到您的應用程序。
節點.js
// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
.generateSignInWithEmailLink(useremail, actionCodeSettings)
.then((link) => {
// Construct sign-in with email link template, embed the link and
// send using custom SMTP server.
return sendSignInEmail(useremail, displayName, link);
})
.catch((error) => {
// Some error occurred.
});
爪哇
String email = "user@example.com";
try {
String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
System.out.println("Error generating email link: " + e.getMessage());
}
Python
email = 'user@example.com'
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
去
email := "user@example.com"
link, err := client.EmailSignInLink(ctx, email, actionCodeSettings)
if err != nil {
log.Fatalf("error generating email link: %v\n", err)
}
// Construct sign-in with email link template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)
C#
var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);
生成鏈接後,可以將其插入到自定義登錄電子郵件中,然後使用自定義 SMTP 服務器通過電子郵件發送給相應的用戶。
詳細了解如何使用電子郵件鏈接通過 Firebase 對用戶進行身份驗證。這將有助於提供有關在用戶單擊鏈接並被重定向回應用程序後如何完成登錄的信息。