移动应用程序有时需要与用户交互,并通过发送电子邮件提示他们采取某些行动。
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 对用户进行身份验证。这将有助于提供有关在用户单击链接并被重定向回应用程序后如何完成登录的信息。