บางครั้งแอปบนอุปกรณ์เคลื่อนที่จำเป็นต้องโต้ตอบกับผู้ใช้และแจ้งให้พวกเขาดำเนินการบางอย่างโดยการส่งอีเมล
Firebase Client SDK ช่วยให้สามารถส่งอีเมลถึงผู้ใช้ที่มีลิงก์ที่สามารถใช้เพื่อรีเซ็ตรหัสผ่าน การยืนยันที่อยู่อีเมล และการลงชื่อเข้าใช้ผ่านอีเมล Google ส่งอีเมลที่ใช้เทมเพลตเหล่านี้และมีความสามารถในการปรับแต่งที่จำกัด
หากคุณต้องการใช้เทมเพลตอีเมลและบริการส่งอีเมลของคุณเองแทน หน้านี้อธิบายวิธีใช้ Firebase Admin SDK เพื่อสร้างลิงก์การดำเนินการโดยทางโปรแกรมสำหรับขั้นตอนข้างต้น ซึ่งคุณสามารถรวมไว้ในอีเมลที่ส่งถึงผู้ใช้ของคุณได้
ซึ่งมาพร้อมกับสิทธิประโยชน์ดังต่อไปนี้:
- ปรับแต่งเทมเพลตอีเมล ซึ่งรวมถึงความสามารถในการเพิ่มสไตล์ใหม่และการสร้างแบรนด์ที่กำหนดเอง เปลี่ยนถ้อยคำและโลโก้ กล่าวถึงผู้ใช้ด้วยชื่อแทนชื่อเต็ม และอื่นๆ
- ใช้เทมเพลตที่แตกต่างกันขึ้นอยู่กับบริบท ตัวอย่างเช่น หากผู้ใช้กำลังยืนยันอีเมลเพื่อสมัครรับจดหมายข่าว อาจจำเป็นต้องระบุบริบทในเนื้อหาอีเมล อีกตัวอย่างหนึ่งคือการลงชื่อเข้าใช้ลิงก์อีเมล: ในสถานการณ์หนึ่ง สิ่งนี้อาจถูกทริกเกอร์โดยผู้ใช้คนเดียวกัน หรือเป็นการเชิญโดยผู้ใช้รายอื่น บริบทจะต้องรวมอยู่ในอีเมล
- แปลเทมเพลตอีเมลแบบกำหนดเองเป็นภาษาท้องถิ่น
- ความสามารถในการสร้างลิงค์จากสภาพแวดล้อมเซิร์ฟเวอร์ที่ปลอดภัย
- ความสามารถในการปรับแต่งวิธีการเปิดลิงก์ ผ่านแอปมือถือหรือเบราว์เซอร์ และวิธีการส่งผ่านข้อมูลสถานะเพิ่มเติม ฯลฯ
- ความสามารถในการปรับแต่งโดเมนลิงก์ไดนามิกที่ใช้สำหรับโฟลว์แอปบนมือถือเมื่อสร้างลิงก์การดำเนินการอีเมล และแม้แต่ระบุโดเมนลิงก์ไดนามิกอื่นโดยขึ้นอยู่กับบริบทหรือแอปมือถือ
เริ่มต้นการตั้งค่า ActionCode
ก่อนที่คุณจะสามารถสร้างลิงก์การดำเนินการทางอีเมลได้ คุณอาจต้องเริ่มต้น ActionCodeSettings
ActionCodeSettings
ช่วยให้คุณสามารถส่งสถานะเพิ่มเติมผ่าน URL ดำเนินการต่อซึ่งสามารถเข้าถึงได้หลังจากที่ผู้ใช้คลิกลิงก์อีเมล นอกจากนี้ยังช่วยให้ผู้ใช้สามารถกลับไปที่แอปได้หลังจากดำเนินการเสร็จสิ้นแล้ว นอกจากนี้ คุณยังสามารถระบุได้ว่าจะจัดการลิงก์การดำเนินการอีเมลโดยตรงจากแอปพลิเคชันมือถือเมื่อติดตั้งหรือจากเบราว์เซอร์
สำหรับลิงก์ที่มีไว้เพื่อเปิดผ่านแอปบนอุปกรณ์เคลื่อนที่ คุณจะต้องเปิดใช้งาน Firebase Dynamic Links และดำเนินการบางอย่างเพื่อตรวจหาลิงก์เหล่านี้จากแอปบนอุปกรณ์เคลื่อนที่ของคุณ โปรดดูคำแนะนำเกี่ยวกับวิธี กำหนดค่า Firebase Dynamic Links สำหรับการดำเนินการกับอีเมล
หากต้องการเริ่มต้นอินสแตนซ์ ActionCodeSettings
ให้ระบุข้อมูลต่อไปนี้:
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
url | เชือก | ตั้งค่าลิงก์ (สถานะ/URL ต่อไป) ซึ่งมีความหมายที่แตกต่างกันในบริบทที่แตกต่างกัน:
|
iOS | ({bundleId: string}|ไม่ได้กำหนด) | ตั้งค่า ID บันเดิล การดำเนินการนี้จะพยายามเปิดลิงก์ในแอป Apple หากมีการติดตั้งไว้ แอปจะต้องลงทะเบียนในคอนโซล |
android | ({ชื่อแพ็คเกจ: สตริง, installApp:บูลีน|ไม่ได้กำหนด, เวอร์ชันต่ำสุด: สตริง|ไม่ได้กำหนด}|ไม่ได้กำหนด) | ตั้งชื่อแพ็คเกจ Android การดำเนินการนี้จะพยายามเปิดลิงก์ในแอป Android หากติดตั้งไว้ หากผ่าน installApp จะระบุว่าจะติดตั้งแอป Android หากอุปกรณ์รองรับและยังไม่ได้ติดตั้งแอป หากระบุช่องนี้โดยไม่มี packageName ระบบจะแสดงข้อผิดพลาดโดยอธิบายว่าต้องระบุ packageName ร่วมกับช่องนี้ หากมีการระบุ minimumVersion และติดตั้งแอปเวอร์ชันเก่า ระบบจะนำผู้ใช้ไปที่ Play Store เพื่ออัปเกรดแอป แอป Android จำเป็นต้องลงทะเบียนในคอนโซล |
handleCodeInApp | (บูลีน|ไม่ได้กำหนด) | ไม่ว่าลิงก์การดำเนินการอีเมลจะเปิดในแอปบนมือถือหรือเว็บลิงก์ก่อน ค่าเริ่มต้นเป็นเท็จ เมื่อตั้งค่าเป็นจริง ลิงก์โค้ดการดำเนินการจะถูกส่งเป็น Universal Link หรือ Android App Link และจะเปิดโดยแอปหากติดตั้งไว้ ในกรณีที่เป็นเท็จ โค้ดจะถูกส่งไปยังเว็บวิดเจ็ตก่อน จากนั้นดำเนินการต่อจะเปลี่ยนเส้นทางไปยังแอปหากติดตั้งไว้ |
dynamicLinkDomain | (สตริง|ไม่ได้กำหนด) | ตั้งค่าโดเมนลิงก์แบบไดนามิก (หรือโดเมนย่อย) เพื่อใช้สำหรับลิงก์ปัจจุบัน หากต้องการเปิดโดยใช้ Firebase Dynamic Links เนื่องจากสามารถกำหนดค่าโดเมนลิงก์แบบไดนามิกได้หลายโดเมนต่อโปรเจ็กต์ ฟิลด์นี้จึงทำให้สามารถเลือกโดเมนได้อย่างชัดเจน หากไม่มีการระบุไว้ ระบบจะใช้โดเมนที่เก่าที่สุดเป็นค่าเริ่มต้น |
ตัวอย่างต่อไปนี้แสดงวิธีส่งลิงก์ยืนยันอีเมลที่จะเปิดในแอปบนอุปกรณ์เคลื่อนที่ก่อนเป็น 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
โหนด 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',
};
ชวา
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();
หลาม
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",
};
หากต้องการเรียนรู้เพิ่มเติม โปรดดูที่ สถานะการส่งผ่านในการดำเนินการกับอีเมล
สร้างลิงก์อีเมลรีเซ็ตรหัสผ่าน
หากต้องการสร้างลิงก์รีเซ็ตรหัสผ่าน ให้ระบุอีเมลของผู้ใช้ที่มีอยู่และออบเจ็กต์ ActionCodeSettings
ที่เป็นตัวเลือก การดำเนินการจะได้รับการแก้ไขด้วยลิงก์การดำเนินการทางอีเมล อีเมลที่ใช้ต้องเป็นของผู้ใช้ที่มีอยู่
โหนด 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.
});
ชวา
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());
}
หลาม
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 แบบกำหนดเอง
หากคุณไม่ได้ใช้หน้าเริ่มต้นของการรีเซ็ตรหัสผ่านและสร้างตัวจัดการแบบกำหนดเองของคุณเอง โปรดดูที่ การสร้างตัวจัดการการดำเนินการอีเมลแบบกำหนดเอง
สร้างลิงค์ยืนยันอีเมล
หากต้องการสร้างลิงก์การยืนยันอีเมล ให้ระบุอีเมลที่ยังไม่ยืนยันของผู้ใช้ที่มีอยู่และออบเจ็กต์ ActionCodeSettings
ที่เป็นตัวเลือก การดำเนินการจะได้รับการแก้ไขด้วยลิงก์การดำเนินการทางอีเมล อีเมลที่ใช้ต้องเป็นของผู้ใช้ที่มีอยู่
โหนด 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.
});
ชวา
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());
}
หลาม
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 แบบกำหนดเอง
หากคุณไม่ได้ใช้หน้า Landing Page ของการยืนยันอีเมลเริ่มต้นและสร้างตัวจัดการที่กำหนดเอง โปรดดูที่ การสร้างตัวจัดการการดำเนินการอีเมลแบบกำหนดเอง
สร้างลิงก์อีเมลสำหรับการลงชื่อเข้าใช้
ก่อนที่คุณจะสามารถตรวจสอบสิทธิ์ผู้ใช้ด้วยการลงชื่อเข้าใช้ลิงก์อีเมล คุณจะต้อง เปิดใช้งานการลงชื่อเข้าใช้ลิงก์อีเมล สำหรับโปรเจ็กต์ Firebase ของคุณ
หากต้องการสร้างลิงก์ลงชื่อเข้าใช้ ให้ระบุอีเมลของผู้ใช้และออบเจ็ ActionCodeSettings
ในกรณีนี้ จำเป็นต้องมีออบเจ็กต์ ActionCodeSettings
เพื่อให้ข้อมูลเกี่ยวกับตำแหน่งที่จะส่งคืนผู้ใช้หลังจากคลิกลิงก์เพื่อการลงชื่อเข้าใช้ให้เสร็จสิ้น การดำเนินการจะได้รับการแก้ไขด้วยลิงก์การดำเนินการทางอีเมล
แตกต่างจากการรีเซ็ตรหัสผ่านและการยืนยันอีเมล อีเมลที่ใช้ไม่จำเป็นต้องเป็นของผู้ใช้ที่มีอยู่ เนื่องจากการดำเนินการนี้สามารถใช้เพื่อลงทะเบียนผู้ใช้ใหม่เข้าสู่แอปของคุณผ่านลิงก์อีเมล
โหนด 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.
});
ชวา
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());
}
หลาม
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 โดยใช้ลิงก์อีเมล ข้อมูลนี้จะช่วยให้ข้อมูลเกี่ยวกับวิธีการลงชื่อเข้าใช้ให้เสร็จสมบูรณ์หลังจากที่ผู้ใช้คลิกลิงก์และถูกเปลี่ยนเส้นทางกลับไปที่แอป