アカウントに複数の認証プロバイダをリンクする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
既存のユーザー アカウントに認証プロバイダの認証情報をリンクすると、ユーザーが複数の認証プロバイダを使用してアプリにログインできるようになります。ログインに使用した認証プロバイダに関係なく、同じ Firebase ユーザー ID でユーザーを識別できます。たとえば、パスワードを使用してログインしたユーザーが Google アカウントの認証情報をリンクすると、それ以降はどちらの方法でもログインできるようになります。また、匿名ユーザーが Facebook アカウントの認証情報をリンクすると、それ以降は Facebook を使ってログインしてアプリを使用できるようになります。
始める前に
アプリに複数の認証プロバイダ(匿名認証も含む)のサポートを追加します。
ユーザー アカウントに認証プロバイダの認証情報をリンクする
既存のユーザー アカウントに認証プロバイダの認証情報をリンクするには:
任意の認証プロバイダや認証方法を使用してユーザーのログインを行います。
新しい認証プロバイダのログインフローを行います(ただし、いずれかの signInWith
メソッドを呼び出す手前まで)。たとえば、ユーザーの Google ID トークン、Facebook アクセス トークン、またはメールアドレスとパスワードを取得します。
新しい認証プロバイダの Credential
オブジェクトを取得します。
// Google Sign-in
final credential = GoogleAuthProvider.credential(idToken: idToken);
// Email and password sign-in
final credential =
EmailAuthProvider.credential(email: emailAddress, password: password);
// Etc.
ログイン ユーザーの linkWithCredential()
メソッドに Credential
オブジェクトを渡します。
try {
final userCredential = await FirebaseAuth.instance.currentUser
?.linkWithCredential(credential);
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "provider-already-linked":
print("The provider has already been linked to the user.");
break;
case "invalid-credential":
print("The provider's credential is not valid.");
break;
case "credential-already-in-use":
print("The account corresponding to the credential already exists, "
"or is already linked to a Firebase User.");
break;
// See the API reference for the full list of error codes.
default:
print("Unknown error.");
}
```
linkWithCredential()
の呼び出しが成功したら、ユーザーはリンクされた認証プロバイダを使用してログインし、同じ Firebase データにアクセスできるようになります。
ユーザー アカウントから認証プロバイダのリンクを解除する
アカウントから認証プロバイダのリンクを解除することで、ユーザーがそのプロバイダを使用してログインできなくなります。
ユーザー アカウントから認証プロバイダのリンクを解除するには、プロバイダ ID を unlink()
メソッドに渡します。ユーザーにリンクされている認証プロバイダのプロバイダ ID は User
オブジェクトの providerData
プロパティから取得できます。
try {
await FirebaseAuth.instance.currentUser?.unlink(providerId);
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "no-such-provider":
print("The user isn't linked to the provider or the provider "
"doesn't exist.");
break;
default:
print("Unknown error.");
}
}
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-04 UTC。
[null,null,["最終更新日 2025-08-04 UTC。"],[],[],null,["\u003cbr /\u003e\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by linking auth provider credentials to an existing user account.\nUsers are identifiable by the same Firebase user ID regardless of the\nauthentication provider they used to sign in. For example, a user who signed in\nwith a password can link a Google account and sign in with either method in the\nfuture. Or, an anonymous user can link a Facebook account and then, later, sign\nin with Facebook to continue using your app.\n\nBefore you begin\n\nAdd support for two or more authentication providers (possibly including\nanonymous authentication) to your app.\n\nLink auth provider credentials to a user account\n\nTo link auth provider credentials to an existing user account:\n\n1. Sign in the user using any authentication provider or method.\n\n2. Complete the sign-in flow for the new authentication provider up to, but not\n including, calling one of the `signInWith`- methods. For example, get\n the user's Google ID token, Facebook access token, or email and password.\n\n3. Get a `Credential` object for the new authentication provider:\n\n // Google Sign-in\n final credential = GoogleAuthProvider.credential(idToken: idToken);\n\n // Email and password sign-in\n final credential =\n EmailAuthProvider.credential(email: emailAddress, password: password);\n\n // Etc.\n\n4. Pass the `Credential` object to the sign-in user's `linkWithCredential()`\n method:\n\n try {\n final userCredential = await FirebaseAuth.instance.currentUser\n ?.linkWithCredential(credential);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"provider-already-linked\":\n print(\"The provider has already been linked to the user.\");\n break;\n case \"invalid-credential\":\n print(\"The provider's credential is not valid.\");\n break;\n case \"credential-already-in-use\":\n print(\"The account corresponding to the credential already exists, \"\n \"or is already linked to a Firebase User.\");\n break;\n // See the API reference for the full list of error codes.\n default:\n print(\"Unknown error.\");\n }\n ```\n\nIf the call to `linkWithCredential()` succeeds, the user can now sign in using\nany linked authentication provider and access the same Firebase data.\n\nUnlink an auth provider from a user account\n\nYou can unlink an auth provider from an account, so that the user can no\nlonger sign in with that provider.\n\nTo unlink an auth provider from a user account, pass the provider ID to the\n`unlink()` method. You can get the provider IDs of the auth providers linked to\na user from the `User` object's `providerData` property. \n\n try {\n await FirebaseAuth.instance.currentUser?.unlink(providerId);\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"no-such-provider\":\n print(\"The user isn't linked to the provider or the provider \"\n \"doesn't exist.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }"]]