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

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

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

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

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

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

เริ่มต้น ActionCodeSettings

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

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

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

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

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

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

  • เมื่อจัดการลิงก์ในวิดเจ็ตการดำเนินการบนเว็บ นี่จะเป็น ในพารามิเตอร์การค้นหา continueUrl
  • เมื่อจัดการลิงก์ในแอปโดยตรง จะเกิดสิ่งต่อไปนี้ พารามิเตอร์การค้นหา continueUrl รายการใน Deep Link ของ ลิงก์แบบไดนามิก
iOS ({bundleId: string}|ไม่ระบุ) ตั้งค่ารหัสชุด การดำเนินการนี้จะพยายามเปิดลิงก์ในแอป Apple หาก ติดตั้งแล้ว ต้องลงทะเบียนแอปในคอนโซล
android ({packageName: string, installApp:boolean|undefined เวอร์ชันต่ำสุด: string|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 Dynamic ลิงก์

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