カスタム認証システムを使用して Firebase 認証を行う
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
ユーザーがログインに成功したときに署名済みのカスタム トークンが生成されるように認証サーバーを変更することで、Firebase Authentication をカスタム認証システムと統合できます。アプリはこのトークンを受信して、Firebase での認証に使用します。
始める前に
- まだ行っていない場合は、スタートガイドの手順に沿って操作してください。
- Firebase Admin SDK をインストールして構成します。Firebase プロジェクトの正しい認証情報を使用して SDK を初期化してください。
Firebase で認証する
ユーザーがアプリにログインしたら、そのユーザーのログイン認証情報(たとえば、ユーザー名とパスワード)を認証サーバーに送信します。サーバーにより認証情報が確認され、認証情報が有効な場合は、Firebase カスタム トークンを作成して、トークンをアプリに返します。
認証サーバーからカスタム トークンを受信したら、signInWithCustomToken()
に渡してユーザーのログインを行います。
try {
final userCredential =
await FirebaseAuth.instance.signInWithCustomToken(token);
print("Sign-in successful.");
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "invalid-custom-token":
print("The supplied token is not a Firebase custom auth token.");
break;
case "custom-token-mismatch":
print("The supplied token is for a different Firebase project.");
break;
default:
print("Unknown error.");
}
}
次のステップ
ユーザーが作成した新しいアカウントは Firebase プロジェクトの一部として保存されます。このアカウントは、ユーザーが使用するログイン方法に関係なく、プロジェクトのすべてのアプリでユーザーの識別に使用できます。
アプリでは、User
オブジェクトからユーザーの基本的なプロフィール情報を取得します。ユーザーを管理するをご覧ください。
Firebase Realtime Database と Cloud Storage のセキュリティ ルールでは、ログイン済みユーザーの一意のユーザー ID を auth
変数から取得し、それを使用して、ユーザーがアクセス可能なデータを制御できます。
既存のユーザー アカウントに認証プロバイダの認証情報をリンクすることで、ユーザーは複数の認証プロバイダを使用してアプリにログインできるようになります。
ユーザーのログアウトを行うには、signOut()
を呼び出します。
await FirebaseAuth.instance.signOut();
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-04 UTC。
[null,null,["最終更新日 2025-08-04 UTC。"],[],[],null,["# Authenticate with Firebase Using a Custom Authentication System\n\n\u003cbr /\u003e\n\nYou can integrate Firebase Authentication with a custom authentication system by\nmodifying your authentication server to produce custom signed tokens when a user\nsuccessfully signs in. Your app receives this token and uses it to authenticate\nwith Firebase.\n\nBefore you begin\n----------------\n\n1. If you haven't already, follow the steps in the [Get started](/docs/auth/flutter/start) guide.\n2. [Install and configure the Firebase Admin SDK](/docs/admin/setup). Be sure to [initialize the SDK](/docs/admin/setup#initialize-sdk) with the correct credentials for your Firebase project.\n\nAuthenticate with Firebase\n--------------------------\n\n1. When users sign in to your app, send their sign-in credentials (for\n example, their username and password) to your authentication server. Your\n server checks the credentials and, if they are valid,\n [creates a custom Firebase token](/docs/auth/admin/create-custom-tokens)\n and sends the token back to your app.\n\n2. After you receive the custom token from your authentication server, pass it\n to `signInWithCustomToken()` to sign in the user:\n\n try {\n final userCredential =\n await FirebaseAuth.instance.signInWithCustomToken(token);\n print(\"Sign-in successful.\");\n } on FirebaseAuthException catch (e) {\n switch (e.code) {\n case \"invalid-custom-token\":\n print(\"The supplied token is not a Firebase custom auth token.\");\n break;\n case \"custom-token-mismatch\":\n print(\"The supplied token is for a different Firebase project.\");\n break;\n default:\n print(\"Unknown error.\");\n }\n }\n\nNext steps\n----------\n\nAfter a user creates a new account, this account is stored as part of your\nFirebase project, and can be used to identify a user across every app in your\nproject, regardless of what sign-in method the user used.\n\nIn your apps, you can get the user's basic profile information from the\n`User` object. See [Manage Users](/docs/auth/flutter/manage-users).\n\nIn your Firebase Realtime Database and Cloud Storage Security Rules, you can\nget the signed-in user's unique user ID from the `auth` variable, and use it to\ncontrol what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials](/docs/auth/flutter/account-linking)) to an\nexisting user account.\n\nTo sign out a user, call `signOut()`: \n\n await FirebaseAuth.instance.signOut();"]]