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

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

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

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

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

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

เริ่มต้น ActionCodeSettings

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

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

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

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

พารามิเตอร์ ประเภท คำอธิบาย
url string

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

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

ตัวอย่างต่อไปนี้แสดงวิธีส่งลิงก์สำหรับการยืนยันอีเมลซึ่งจะเปิดในแอปบนอุปกรณ์เคลื่อนที่ก่อนเป็นลิงก์แบบไดนามิกของ Firebase (แอป Apple com.example.ios หรือแอป Android com.example.android ซึ่งแอปจะติดตั้งหากยังไม่ได้ติดตั้ง และเวอร์ชันขั้นต่ำคือ 12) Deep Link จะมีเพย์โหลด 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',
};

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',
)

Go

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

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)

Go

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

หากไม่ได้ใช้หน้า 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)

Go

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

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

คุณจะต้องเปิดใช้การลงชื่อเข้าใช้ลิงก์อีเมลสำหรับโปรเจ็กต์ 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)

Go

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