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

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

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

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

การดำเนินการนี้มาพร้อมกับสิทธิประโยชน์ดังนี้

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

เริ่มต้น ActionCodeSettings

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

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

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

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

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

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

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

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

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