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

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

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

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

ซึ่งมีประโยชน์ดังต่อไปนี้

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

เริ่มต้น ActionCodeSettings

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

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

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

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

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

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

  • เมื่อมีการจัดการลิงก์ในวิดเจ็ตการดำเนินการบนเว็บ นี่คือ Deep Link ในพารามิเตอร์การค้นหา continueUrl
  • เมื่อมีการจัดการลิงก์ในแอปโดยตรง พารามิเตอร์นี้จะเป็นcontinueUrlพารามิเตอร์การค้นหาใน Deep Link ของHostingลิงก์
iOS ({bundleId: string}|undefined) ตั้งค่า Bundle ID ของ iOS เพื่อช่วย Firebase Authentication ระบุว่าควรสร้างลิงก์บนเว็บเท่านั้นหรือลิงก์บนอุปกรณ์เคลื่อนที่ซึ่งเปิดในอุปกรณ์ Apple
android ({packageName: string, installApp:boolean|undefined, minimumVersion: string|undefined}|undefined) ตั้งค่าชื่อแพ็กเกจ Android เพื่อช่วย Firebase Authentication ในการพิจารณาว่าควรสร้างลิงก์สำหรับเว็บเท่านั้นหรือลิงก์สำหรับอุปกรณ์เคลื่อนที่ซึ่งเปิดในอุปกรณ์ Android
handleCodeInApp (บูลีน|ไม่ระบุ) ไม่ว่าจะเปิดลิงก์การดำเนินการในอีเมลในแอปบนอุปกรณ์เคลื่อนที่หรือลิงก์เว็บก่อน ค่าเริ่มต้นคือ false เมื่อตั้งค่าเป็น "จริง" ระบบจะส่งลิงก์รหัสการดำเนินการ เป็น Universal Link หรือ App Link ของ Android และแอปจะเปิดลิงก์ หากติดตั้งไว้ ในกรณีที่เป็นเท็จ ระบบจะส่งรหัสไปยัง วิดเจ็ตบนเว็บก่อน จากนั้นเมื่อดำเนินการต่อ ระบบจะเปลี่ยนเส้นทางไปยังแอปหาก ติดตั้งไว้
linkDomain (string|undefined) เมื่อกำหนดโดเมนลิงก์การโฮสต์ที่กำหนดเองสำหรับโปรเจ็กต์ ให้ระบุโดเมนที่จะใช้เมื่อแอปบนอุปกรณ์เคลื่อนที่ที่ระบุ จะเปิดลิงก์ มิฉะนั้น ระบบจะเลือกโดเมนเริ่มต้นโดยอัตโนมัติ (เช่น PROJECT_ID.firebaseapp.com)
dynamicLinkDomain (string|undefined) เลิกใช้งานแล้ว อย่าระบุพารามิเตอร์นี้

ตัวอย่างต่อไปนี้แสดงวิธีส่งลิงก์ยืนยันทางอีเมลที่จะเปิดในแอปบนอุปกรณ์เคลื่อนที่ก่อน Deep Link จะมีเพย์โหลด URL ของการดำเนินการต่อ https://www.example.com/checkout?cartId=1234 โดเมนลิงก์ Hosting ที่กำหนดเองที่ใช้คือ custom-domain.com ซึ่งต้องกำหนดค่าเพื่อใช้กับ Firebase Hosting

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',
  },
  // The domain must be configured in Firebase Hosting and owned by the project.
  linkDomain: 'custom-domain.com',
};

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