アカウントのリンクとプロバイダー管理
認証プロバイダーをリンク・リンク解除し、競合を処理して、リンク済みの認証情報を安全に管理することで、1人のユーザーが複数のサインイン方法で1つのアカウントを利用できるようにします。
「アカウントのリンクとプロバイダー管理」はCoddyKit上の無料Firebase Auth & Realtime Database Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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
よくある質問
「アカウントのリンクとプロバイダー管理」レッスンは無料ですか?
はい。「アカウントのリンクとプロバイダー管理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Firebase Auth & Realtime Database Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。
「アカウントのリンクとプロバイダー管理」で何を学びますか?
認証プロバイダーをリンク・リンク解除し、競合を処理して、リンク済みの認証情報を安全に管理することで、1人のユーザーが複数のサインイン方法で1つのアカウントを利用できるようにします。 ブラウザで直接実行するハンズオンコードでFirebase Auth & Realtime Database Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Firebase Auth & Realtime Database Appsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFirebase Auth & Realtime Database Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アカウントのリンクとプロバイダー管理」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFirebase Auth & Realtime Database Appsレッスンでコードを書いて実行できますか?
はい。すべてのFirebase Auth & Realtime Database Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 電話番号認証
- 多要素認証(MFA)
- カスタムクレームとセキュリティルール
- アカウントのリンクとプロバイダー管理