การสร้างลิงก์การดำเนินการอีเมล

แอพมือถือบางครั้งจำเป็นต้องโต้ตอบกับผู้ใช้และแจ้งให้พวกเขาดำเนินการบางอย่างโดยการส่งอีเมล

Firebase Client SDK ให้ความสามารถในการส่งอีเมลที่มีลิงก์ที่ผู้ใช้สามารถใช้สำหรับการรีเซ็ตรหัสผ่าน การยืนยันที่อยู่อีเมล และการลงชื่อเข้าใช้ด้วยอีเมล อีเมลที่ใช้เทมเพลตเหล่านี้ส่งโดย Google และมีข้อจำกัดในการปรับแต่ง

หากคุณต้องการใช้เทมเพลตอีเมลและบริการส่งอีเมลของคุณเองแทน หน้านี้อธิบายวิธีใช้ Firebase Admin SDK เพื่อสร้างลิงก์การดำเนินการตามโปรแกรมสำหรับขั้นตอนข้างต้น ซึ่งคุณรวมไว้ในอีเมลถึงผู้ใช้ได้

นี้มาพร้อมกับประโยชน์ดังต่อไปนี้:

  • ปรับแต่งเทมเพลตอีเมล ซึ่งรวมถึงความสามารถในการเพิ่มสไตล์ใหม่ๆ และการสร้างแบรนด์แบบกำหนดเอง เปลี่ยนคำและโลโก้ ระบุผู้ใช้ด้วยชื่อแทนชื่อเต็ม และอื่นๆ
  • ใช้เทมเพลตที่แตกต่างกันขึ้นอยู่กับบริบท ตัวอย่างเช่น หากผู้ใช้ยืนยันอีเมลเพื่อสมัครรับจดหมายข่าว บริบทอาจต้องระบุในเนื้อหาอีเมล อีกตัวอย่างหนึ่งคือการลงชื่อเข้าใช้ลิงก์อีเมล: ในสถานการณ์หนึ่ง ผู้ใช้รายเดียวกันอาจทริกเกอร์สิ่งนี้ หรือเป็นการเชิญโดยผู้ใช้รายอื่น บริบทจะต้องรวมอยู่ในอีเมล
  • แปลเทมเพลตอีเมลที่กำหนดเอง
  • ความสามารถในการสร้างลิงค์จากสภาพแวดล้อมเซิร์ฟเวอร์ที่ปลอดภัย
  • ความสามารถในการปรับแต่งวิธีการเปิดลิงก์ ผ่านแอพมือถือหรือเบราว์เซอร์ และวิธีส่งผ่านข้อมูลสถานะเพิ่มเติม ฯลฯ
  • ความสามารถในการปรับแต่งโดเมนลิงก์แบบไดนามิกที่ใช้สำหรับโฟลว์แอปบนอุปกรณ์เคลื่อนที่เมื่อสร้างลิงก์การดำเนินการอีเมล และแม้กระทั่งระบุโดเมนลิงก์แบบไดนามิกที่แตกต่างกันตามบริบทหรือแอปบนอุปกรณ์เคลื่อนที่

เริ่มต้น ActionCodeSettings

ก่อนที่คุณจะสร้างลิงก์การดำเนินการกับอีเมลได้ คุณอาจต้องเริ่มต้นอินสแตนซ์ ActionCodeSettings

ActionCodeSettings ช่วยให้คุณสามารถส่งผ่านสถานะเพิ่มเติมผ่าน URL ดำเนินการต่อซึ่งสามารถเข้าถึงได้หลังจากที่ผู้ใช้คลิกลิงก์อีเมล นอกจากนี้ยังช่วยให้ผู้ใช้สามารถกลับไปที่แอปได้หลังจากดำเนินการเสร็จสิ้น นอกจากนี้ คุณสามารถระบุว่าจะจัดการลิงก์การดำเนินการอีเมลโดยตรงจากแอปพลิเคชันมือถือเมื่อติดตั้งหรือจากเบราว์เซอร์

สำหรับลิงก์ที่ตั้งใจจะเปิดผ่านแอปบนอุปกรณ์เคลื่อนที่ คุณจะต้องเปิดใช้ลิงก์แบบไดนามิกของ Firebase และดำเนินการบางอย่างเพื่อตรวจหาลิงก์เหล่านี้จากแอปบนอุปกรณ์เคลื่อนที่ของคุณ ดูคำแนะนำเกี่ยวกับวิธี กำหนดค่าลิงก์แบบไดนามิกของ Firebase สำหรับการดำเนินการกับอีเมล

ในการเริ่มต้นอินสแตนซ์ ActionCodeSettings ให้ระบุข้อมูลต่อไปนี้:

พารามิเตอร์ พิมพ์ คำอธิบาย
url สตริง

ตั้งค่าลิงค์ (สถานะ/URL ต่อ) ซึ่งมีความหมายต่างกันในบริบทที่แตกต่างกัน:

  • เมื่อลิงก์ได้รับการจัดการในวิดเจ็ตการดำเนินการบนเว็บ นี่คือลิงก์ในรายละเอียดในพารามิเตอร์ continueUrl สืบค้น ContinueUrl
  • เมื่อมีการจัดการลิงก์ในแอปโดยตรง พารามิเตอร์นี้จะเป็นพารามิเตอร์ continueUrl สืบค้น ContinueUrl ในลิงก์ในรายละเอียดของลิงก์แบบไดนามิก
iOS ({bundleId: string}|undefined) ตั้งค่า ID บันเดิล การดำเนินการนี้จะพยายามเปิดลิงก์ในแอป Apple หากติดตั้งไว้ แอปต้องลงทะเบียนในคอนโซล
android ({packageName: string, installApp:boolean|undefined, maximumVersion: string|undefined}|undefined) ตั้งชื่อแพ็คเกจ Android การดำเนินการนี้จะพยายามเปิดลิงก์ในแอป Android หากติดตั้งไว้ หากผ่าน installApp จะระบุว่าจะติดตั้งแอพ Android หรือไม่หากอุปกรณ์รองรับและยังไม่ได้ติดตั้งแอพ หากระบุฟิลด์นี้โดยไม่มี packageName จะมีข้อผิดพลาดเกิดขึ้นโดยอธิบายว่าต้องระบุ packageName ร่วมกับฟิลด์นี้ หากมีการระบุ minimumVersion และติดตั้งแอปเวอร์ชันเก่ากว่าไว้ ระบบจะนำผู้ใช้ไปที่ Play Store เพื่ออัปเกรดแอป แอป Android จะต้องลงทะเบียนในคอนโซล
handleCodeInApp (บูลีน|ไม่ได้กำหนด) ลิงก์การดำเนินการอีเมลจะเปิดในแอปบนอุปกรณ์เคลื่อนที่หรือลิงก์ของเว็บก่อน ค่าเริ่มต้นเป็นเท็จ เมื่อตั้งค่าเป็น "จริง" ลิงก์โค้ดการดำเนินการจะถูกส่งเป็นลิงก์สากลหรือลิงก์แอป Android และแอปจะเปิดขึ้นหากติดตั้งไว้ ในกรณีเท็จ โค้ดจะถูกส่งไปยังวิดเจ็ตเว็บก่อน จากนั้นดำเนินการต่อจะเปลี่ยนเส้นทางไปยังแอปหากติดตั้งไว้
dynamicLinkDomain (สตริง|ไม่ได้กำหนด) ตั้งค่าโดเมนลิงก์แบบไดนามิก (หรือโดเมนย่อย) เพื่อใช้สำหรับลิงก์ปัจจุบัน หากจะเปิดโดยใช้ลิงก์แบบไดนามิกของ Firebase เนื่องจากสามารถกำหนดค่าโดเมนลิงก์แบบไดนามิกได้หลายรายการต่อโปรเจ็กต์ ฟิลด์นี้จึงมีตัวเลือกให้เลือกอย่างชัดแจ้ง หากไม่มีการระบุ โดเมนที่เก่าที่สุดจะถูกใช้โดยค่าเริ่มต้น

ตัวอย่างต่อไปนี้แสดงวิธีการส่งลิงก์ยืนยันอีเมลที่จะเปิดในแอปบนอุปกรณ์เคลื่อนที่ก่อนเป็น Firebase Dynamic Link (แอป Apple com.example.ios หรือแอป Android com.example.android ซึ่งแอปจะติดตั้งหากยังไม่ได้ติดตั้งและ รุ่นขั้นต่ำคือ 12) ลิงก์ในรายละเอียดจะมีเพย์โหลด URL ต่อ https://www.example.com/checkout?cartId=1234 โดเมนลิงก์แบบไดนามิกที่ใช้คือ coolapp.page.link ซึ่งต้องกำหนดค่าเพื่อใช้กับ Firebase Dynamic Links

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',
};

Java

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",
}

ค#

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",
};

หากต้องการเรียนรู้เพิ่มเติม โปรดดูที่ Passing State ในการดำเนินการกับอีเมล

ในการสร้างลิงก์รีเซ็ตรหัสผ่าน ให้ระบุอีเมลของผู้ใช้ที่มีอยู่และออบเจ็กต์ 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.
  });

Java

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)

ค#

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 ที่กำหนดเอง

หากคุณไม่ได้ใช้หน้า Landing Page รีเซ็ตรหัสผ่านเริ่มต้น และสร้างเครื่องจัดการแบบกำหนดเอง โปรดดู การสร้างเครื่องจัดการการดำเนินการอีเมลที่กำหนดเอง

ในการสร้างลิงก์การยืนยันอีเมล ให้ระบุอีเมลที่ยังไม่ได้ยืนยันของผู้ใช้ที่มีอยู่และออบเจ็กต์ 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.
  });

Java

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)

ค#

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.
  });

Java

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)

ค#

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 โดยใช้ลิงก์อีเมล ข้อมูลนี้จะช่วยให้ข้อมูลเกี่ยวกับวิธีการลงชื่อเข้าใช้ให้สมบูรณ์หลังจากที่ผู้ใช้คลิกลิงก์และเปลี่ยนเส้นทางกลับไปที่แอป