生成電子郵件操作鏈接

行動應用程式有時需要與用戶互動並透過發送電子郵件提示他們採取某些操作。

Firebase用戶端 SDK能夠向使用者發送包含可用於密碼重設、電子郵件地址驗證和基於電子郵件的登入的連結的電子郵件。這些基於範本的電子郵件由 Google 發送,可自訂性有限。

如果您想改用自己的電子郵件範本和自己的電子郵件傳送服務,本頁面介紹如何使用Firebase Admin SDK以程式設計方式產生上述流程的操作鏈接,您可以將其包含在發送給使用者的電子郵件中。

這樣做有以下好處:

  • 自訂電子郵件範本。這包括添加新樣式和自訂品牌、更改措辭和徽標、按名字而不是全名稱呼用戶等的能力。
  • 根據上下文應用不同的模板。例如,如果使用者正在驗證其電子郵件以訂閱時事通訊,則可能需要在電子郵件內容中提供上下文。另一個範例是電子郵件連結登入:在一種情況下,這可能由同一用戶觸發,或作為另一個用戶的邀請觸發。電子郵件中需要包含上下文。
  • 本地化自訂電子郵件範本。
  • 能夠從安全的伺服器環境產生連結。
  • 能夠自訂如何透過行動應用程式或瀏覽器開啟鏈接,以及如何傳遞附加狀態資訊等。
  • 能夠在建立電子郵件操作連結時自訂用於行動應用程式流的動態連結網域,甚至可以根據上下文或行動應用程式指定不同的動態連結網域。

初始化操作程式碼設定

在產生電子郵件操作連結之前,您可能需要初始化ActionCodeSettings實例。

ActionCodeSettings可讓您透過使用者點擊電子郵件連結後可存取的繼續 URL 傳遞其他狀態。這也使用戶能夠在操作完成後返回應用程式。此外,您還可以指定是直接從安裝的行動應用程式還是從瀏覽器處理電子郵件操作連結。

對於要透過行動應用程式開啟的鏈接,您需要啟用 Firebase 動態連結並執行一些任務以從行動應用程式檢測這些連結。請參閱如何為電子郵件操作設定 Firebase 動態連結的說明。

若要初始化ActionCodeSettings實例,請提供以下資料:

範圍類型描述
url細繩

設定在不同上下文中具有不同含義的連結(狀態/繼續 URL):

  • 當在 Web 操作小部件中處理連結時,這是continueUrl查詢參數中的深層連結。
  • 當直接在應用程式中處理連結時,這是動態連結深層連結中的continueUrl查詢參數。
iOS ({bundleId: string}|未定義)設定捆綁包 ID。如果已安裝,這將嘗試在 Apple 應用程式中開啟該連結。該應用程式需要在控制台中註冊。
android ({packageName:字串,installApp:布林|未定義,最小版本:字串|未定義}|未定義)設定 Android 套件名稱。這將嘗試在 Android 應用程式中開啟連結(如果已安裝)。如果傳遞installApp ,則指定如果裝置支援且尚未安裝 Android 應用程序,是否安裝該應用程式。如果提供此欄位時未提供packageName ,則會拋出錯誤,解釋packageName必須與此欄位一起提供。如果指定了minimumVersion ,並且安裝了舊版本的應用程序,用戶將被帶到Play 商店升級應用程式。 Android 應用程式需要在控制台中註冊。
handleCodeInApp (布林值|未定義)電子郵件操作連結是先在行動應用程式中打開,還是先在網頁連結中開啟。預設為 false。當設定為 true 時,操作代碼連結將作為通用連結或 Android 應用程式連結發送,並由應用程式(如果已安裝)開啟。在錯誤的情況下,程式碼將首先發送到網路小部件,然後繼續將重定向到應用程式(如果已安裝)。
dynamicLinkDomain (字串|未定義)如果要使用 Firebase 動態連結開啟目前鏈接,則設定要用於目前連結的動態連結網域(或子網域)。由於每個項目可以配置多個動態連結域,因此該欄位提供了明確選擇一個動態連結域的能力。如果未提供,則預設使用最舊的網域。

以下範例說明如何發送電子郵件驗證鏈接,該鏈接將首先在移動應用程式中作為 Firebase 動態鏈接打開(Apple 應用程式com.example.ios或 Android 應用程式com.example.android ,如果尚未安裝,則將安裝該應用程序,且最低版本為 12)。深層連結將包含繼續 URL 負載https://www.example.com/checkout?cartId=1234 。使用的動態連結網域是coolapp.page.link ,必須將其配置為與 Firebase 動態連結一起使用。

Node.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物件。該操作將透過電子郵件操作連結進行解決。使用的電子郵件必須屬於現有使用者。

Node.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物件。該操作將透過電子郵件操作連結進行解決。使用的電子郵件必須屬於現有使用者。

Node.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物件來提供有關在點擊連結完成登入後將使用者返回到何處的資訊。該操作將透過電子郵件操作連結進行解決。

與密碼重設和電子郵件驗證不同,所使用的電子郵件不一定需要屬於現有用戶,因為此操作可用於透過電子郵件連結將新用戶註冊到您的應用程式中。

Node.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 對使用者進行身份驗證。這將有助於提供有關在用戶點擊連結並重定向回應用程式後如何完成登入的資訊。