การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ
ให้ผู้ใช้หนึ่งคนเป็นเจ้าของบัญชีเดียวที่ใช้ได้กับหลายวิธีเข้าสู่ระบบ ด้วยการเชื่อมโยงและยกเลิกการเชื่อมโยงผู้ให้บริการยืนยันตัวตน การจัดการกรณีชนกัน และการดูแลข้อมูลรับรองที่เชื่อมโยงอย่างปลอดภัย
การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ เป็นบทเรียน Firebase Auth & Realtime Database Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Firebase Auth & Realtime Database Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Multi-Provider Problem
The same person may sign in with email/password today and Google tomorrow. Without linking, Firebase could treat these as two separate accounts.
Account linking unifies multiple sign-in methods under one Firebase user (one uid).
How Linking Works
Linking attaches an additional credential to the currently signed-in user. The user keeps the same uid and data, but gains a new way to log in.
- Email/password can be linked to a social account
- Multiple social providers can coexist
Linking a Provider
Use linkWithPopup on the current user to add another provider interactively.
import { getAuth, GoogleAuthProvider, linkWithPopup } from 'firebase/auth';
const user = getAuth().currentUser;
await linkWithPopup(user, new GoogleAuthProvider());
console.log('Google linked to existing account');Linking a Credential Directly
When you already hold a credential (for example email/password the user just typed), use linkWithCredential.
import { EmailAuthProvider, linkWithCredential } from 'firebase/auth';
const cred = EmailAuthProvider.credential(email, password);
await linkWithCredential(getAuth().currentUser, cred);The Collision Error
If the new provider's email already belongs to a different account, Firebase throws auth/account-exists-with-different-credential. This is the key case you must handle.
Resolving a Collision
To resolve it: read the conflicting email, ask the user to sign in with the existing provider, then link the new credential onto that account.
import { fetchSignInMethodsForEmail } from 'firebase/auth';
const methods = await fetchSignInMethodsForEmail(auth, email);
// prompt user to sign in with methods[0], then link pendingCredInspecting Linked Providers
The user object exposes providerData, an array describing every linked provider. Use it to render a 'connected accounts' settings screen.
const user = getAuth().currentUser;
user.providerData.forEach(p => console.log(p.providerId));Unlinking a Provider
Let users disconnect a method with unlink, passing the provider ID. Always keep at least one sign-in method so the account stays accessible.
import { unlink } from 'firebase/auth';
await unlink(getAuth().currentUser, 'google.com');Guarding the Last Provider
Before unlinking, check that more than one provider remains. Removing the only method would orphan the user.
const user = getAuth().currentUser;
if (user.providerData.length <= 1) {
showError('You must keep at least one sign-in method.');
return;
}Reauthentication for Sensitive Changes
Linking or unlinking is a sensitive operation. If the user's session is old, Firebase may require recent login and throw auth/requires-recent-login. Reauthenticate before retrying.
import { reauthenticateWithPopup } from 'firebase/auth';
await reauthenticateWithPopup(user, new GoogleAuthProvider());Design Considerations
Plan your identity model up front:
- Treat email as the unifying key when possible
- Surface linked accounts in user settings
- Always handle the collision error gracefully
Quick Check
Test your understanding of account linking.
Recap
You can now unify identities across providers.
- Link with
linkWithPopuporlinkWithCredential - Handle
account-exists-with-different-credential - Inspect
providerDataand unlink safely - Always keep at least one provider
- Reauthenticate for sensitive changes
คำถามที่พบบ่อย
บทเรียน “การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Firebase Auth & Realtime Database Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Firebase Auth & Realtime Database Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ”
ให้ผู้ใช้หนึ่งคนเป็นเจ้าของบัญชีเดียวที่ใช้ได้กับหลายวิธีเข้าสู่ระบบ ด้วยการเชื่อมโยงและยกเลิกการเชื่อมโยงผู้ให้บริการยืนยันตัวตน การจัดการกรณีชนกัน และการดูแลข้อมูลรับรองที่เชื่อมโยงอย่างปลอดภัย คุณปฏิบัติ Firebase Auth & Realtime Database Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Firebase Auth & Realtime Database Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Firebase Auth & Realtime Database Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Firebase Auth & Realtime Database Apps นี้ได้ไหม
ได้ บทเรียน Firebase Auth & Realtime Database Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การยืนยันตัวตนด้วยหมายเลขโทรศัพท์
- การยืนยันตัวตนหลายปัจจัย (MFA)
- ข้ออ้างสิทธิ์แบบกำหนดเองและกฎความปลอดภัย
- การเชื่อมโยงบัญชีและการจัดการผู้ให้บริการ