(省略可)Firebase Local Emulator Suite でプロトタイピングとテストを行う
アプリによるユーザーの認証方法について見ていく前に、Authentication 機能のプロトタイピングとテストに使用できるツールである Firebase Local Emulator Suite について紹介します。認証手法やプロバイダを検討している場合や、Authentication と Firebase Security Rules を使用して公開および非公開のデータで各種データモデルを試している場合、さらに、ログイン時の UI デザインのプロトタイプを作成している場合などは、ライブサービスをデプロイせずにローカルで作業できると便利です。
Authentication エミュレータは Local Emulator Suite の一部であり、これを使用すると、アプリはエミュレートされたデータベースのコンテンツと構成とやり取りでき、エミュレートされたプロジェクト リソース(関数、他のデータベース、セキュリティ ルール)ともオプションでやり取りできるようになります。
import{getAuth,createUserWithEmailAndPassword}from"firebase/auth";constauth=getAuth();createUserWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed up constuser=userCredential.user;// ...}).catch((error)=>{consterrorCode=error.code;consterrorMessage=error.message;// ..});
firebase.auth().createUserWithEmailAndPassword(email,password).then((userCredential)=>{// Signed in varuser=userCredential.user;// ...}).catch((error)=>{varerrorCode=error.code;varerrorMessage=error.message;// ..});
import{getAuth,signInWithEmailAndPassword}from"firebase/auth";constauth=getAuth();signInWithEmailAndPassword(auth,email,password).then((userCredential)=>{// Signed in constuser=userCredential.user;// ...}).catch((error)=>{consterrorCode=error.code;consterrorMessage=error.message;});
firebase.auth().signInWithEmailAndPassword(email,password).then((userCredential)=>{// Signed invaruser=userCredential.user;// ...}).catch((error)=>{varerrorCode=error.code;varerrorMessage=error.message;});
import{getAuth,onAuthStateChanged}from"firebase/auth";constauth=getAuth();onAuthStateChanged(auth,(user)=>{if(user){// User is signed in, see docs for a list of available properties// https://firebase.google.com/docs/reference/js/auth.userconstuid=user.uid;// ...}else{// User is signed out// ...}});
firebase.auth().onAuthStateChanged((user)=>{if(user){// User is signed in, see docs for a list of available properties// https://firebase.google.com/docs/reference/js/v8/firebase.Uservaruid=user.uid;// ...}else{// User is signed out// ...}});
[null,null,["最終更新日 2025-08-16 UTC。"],[],[],null,["You can use Firebase Authentication to allow users to sign in to your app using one or\nmore sign-in methods, including email address and password sign-in, and\nfederated identity providers such as Google Sign-in and Facebook Login. This\ntutorial gets you started with Firebase Authentication by showing you how to add\nemail address and password sign-in to your app.\n\nAdd and initialize the Authentication SDK\n\n1. If you haven't already, [install the Firebase JS SDK and initialize Firebase](/docs/web/setup#add-sdk-and-initialize).\n\n2. Add the Firebase Authentication JS SDK and initialize Firebase Authentication:\n\nWeb\n\n\n| [Learn more](/docs/web/learn-more#modular-version) about the tree-shakeable modular web API and [upgrade](/docs/web/modular-upgrade) from the namespaced API.\n\n\u003cbr /\u003e\n\n```python\nimport { initializeApp } from \"firebase/app\";\nimport { getAuth } from \"firebase/auth\";\n\n// TODO: Replace the following with your app's Firebase project configuration\n// See: https://firebase.google.com/docs/web/learn-more#config-object\nconst firebaseConfig = {\n // ...\n};\n\n// Initialize Firebase\nconst app = initializeApp(firebaseConfig);\n\n\n// Initialize Firebase Authentication and get a reference to the service\nconst auth = getAuth(app);\n```\n\nWeb\n\n\n| [Learn more](/docs/web/learn-more#modular-version) about the tree-shakeable modular web API and [upgrade](/docs/web/modular-upgrade) from the namespaced API.\n\n\u003cbr /\u003e\n\n```python\nimport firebase from \"firebase/compat/app\";\nimport \"firebase/compat/auth\";\n\n// TODO: Replace the following with your app's Firebase project configuration\n// See: https://firebase.google.com/docs/web/learn-more#config-object\nconst firebaseConfig = {\n // ...\n};\n\n// Initialize Firebase\nfirebase.initializeApp(firebaseConfig);\n\n\n// Initialize Firebase Authentication and get a reference to the service\nconst auth = firebase.auth();\n```\n\n(Optional) Prototype and test with Firebase Local Emulator Suite\n\nBefore talking about how your app authenticates users, let's introduce a set of\ntools you can use to prototype and test Authentication functionality:\nFirebase Local Emulator Suite. If you're deciding among authentication techniques\nand providers, trying out different data models with public and private data\nusing Authentication and Firebase Security Rules, or prototyping sign-in UI designs, being able to\nwork locally without deploying live services can be a great idea.\n\nAn Authentication emulator is part of the Local Emulator Suite, which\nenables your app to interact with emulated database content and config, as\nwell as optionally your emulated project resources (functions, other databases,\nand security rules).\n\nUsing the Authentication emulator involves just a few steps:\n\n1. Adding a line of code to your app's test config to connect to the emulator.\n2. From the root of your local project directory, running `firebase emulators:start`.\n3. Using the Local Emulator Suite UI for interactive prototyping, or the Authentication emulator REST API for non-interactive testing.\n\nA detailed guide is available at [Connect your app to the Authentication emulator](/docs/emulator-suite/connect_auth).\nFor more information, see the [Local Emulator Suite introduction](/docs/emulator-suite).\n\nNow let's continue with how to authenticate users.\n\nSign up new users\n\nCreate a form that allows new users to register with your app using their email\naddress and a password. When a user completes the form, validate the email\naddress and password provided by the user, then pass them to the\n`createUserWithEmailAndPassword` method: \n\nWeb \n\n```javascript\nimport { getAuth, createUserWithEmailAndPassword } from \"firebase/auth\";\n\nconst auth = getAuth();\ncreateUserWithEmailAndPassword(auth, email, password)\n .then((userCredential) =\u003e {\n // Signed up \n const user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n const errorCode = error.code;\n const errorMessage = error.message;\n // ..\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/auth-next/email/auth_signup_password.js#L8-L21\n```\n\nWeb \n\n```javascript\nfirebase.auth().createUserWithEmailAndPassword(email, password)\n .then((userCredential) =\u003e {\n // Signed in \n var user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n var errorCode = error.code;\n var errorMessage = error.message;\n // ..\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/auth/email.js#L28-L38\n```\n\nSign in existing users\n\nCreate a form that allows existing users to sign in using their email address\nand password. When a user completes the form, call the\n`signInWithEmailAndPassword` method: \n\nWeb \n\n```javascript\nimport { getAuth, signInWithEmailAndPassword } from \"firebase/auth\";\n\nconst auth = getAuth();\nsignInWithEmailAndPassword(auth, email, password)\n .then((userCredential) =\u003e {\n // Signed in \n const user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n const errorCode = error.code;\n const errorMessage = error.message;\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/auth-next/email/auth_signin_password.js#L8-L20\n```\n\nWeb \n\n```javascript\nfirebase.auth().signInWithEmailAndPassword(email, password)\n .then((userCredential) =\u003e {\n // Signed in\n var user = userCredential.user;\n // ...\n })\n .catch((error) =\u003e {\n var errorCode = error.code;\n var errorMessage = error.message;\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/auth/email.js#L11-L20\n```\n\nSet an authentication state observer and get user data\n\nFor each of your app's pages that need information about the signed-in user,\nattach an observer to the global authentication object. This observer gets\ncalled whenever the user's sign-in state changes.\n\nAttach the observer using the `onAuthStateChanged` method. When a user\nsuccessfully signs in, you can get information about the user in the observer. \n\nWeb \n\n```javascript\nimport { getAuth, onAuthStateChanged } from \"firebase/auth\";\n\nconst auth = getAuth();\nonAuthStateChanged(auth, (user) =\u003e {\n if (user) {\n // User is signed in, see docs for a list of available properties\n // https://firebase.google.com/docs/reference/js/auth.user\n const uid = user.uid;\n // ...\n } else {\n // User is signed out\n // ...\n }\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/auth-next/index/auth_state_listener.js#L8-L21\n```\n\nWeb \n\n```javascript\nfirebase.auth().onAuthStateChanged((user) =\u003e {\n if (user) {\n // User is signed in, see docs for a list of available properties\n // https://firebase.google.com/docs/reference/js/v8/firebase.User\n var uid = user.uid;\n // ...\n } else {\n // User is signed out\n // ...\n }\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/auth/index.js#L43-L53\n```\n\nNext steps\n\nLearn how to add support for other identity providers and anonymous guest\naccounts:\n\n- [Google Sign-in](/docs/auth/web/google-signin)\n- [Facebook Login](/docs/auth/web/facebook-login)\n- [Twitter Login](/docs/auth/web/twitter-login)\n- [GitHub Login](/docs/auth/web/github-auth)\n- [Anonymous sign-in](/docs/auth/web/anonymous-auth)"]]