เริ่มต้นใช้งานการตรวจสอบสิทธิ์ Firebase ในเว็บไซต์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณใช้ Firebase Authentication เพื่ออนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปโดยใช้วิธีการลงชื่อเข้าใช้หนึ่งวิธีขึ้นไปได้ ซึ่งรวมถึงการลงชื่อเข้าใช้ด้วยอีเมลและรหัสผ่าน รวมถึงผู้ให้บริการข้อมูลประจำตัวแบบรวมศูนย์ เช่น Google Sign-In และ Facebook Login บทแนะนำนี้จะช่วยให้คุณเริ่มต้นใช้งาน Firebase Authentication โดยแสดงวิธีเพิ่มการลงชื่อเข้าใช้ด้วยอีเมลและรหัสผ่านลงในแอป
เพิ่มและเริ่มต้น Authentication SDK
หากยังไม่ได้ดำเนินการ ให้
ติดตั้ง Firebase JS SDK และเริ่มต้น Firebase
เพิ่ม Firebase Authentication JS SDK และเริ่มต้นใช้งาน Firebase Authentication
Web
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app);
Web
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Initialize Firebase Authentication and get a reference to the service
const auth = firebase.auth();
(ไม่บังคับ) สร้างต้นแบบและทดสอบด้วย Firebase Local Emulator Suite
ก่อนจะพูดถึงวิธีที่แอปของคุณตรวจสอบสิทธิ์ผู้ใช้ เรามาทำความรู้จักชุดเครื่องมือที่คุณใช้ในการสร้างต้นแบบและทดสอบAuthenticationฟังก์ชันการทำงานFirebase Local Emulator Suiteกันก่อน
หากคุณกำลังตัดสินใจเลือกเทคนิคการตรวจสอบสิทธิ์
และผู้ให้บริการ ลองใช้โมเดลข้อมูลต่างๆ กับข้อมูลสาธารณะและข้อมูลส่วนตัว
โดยใช้ Authentication และ Firebase Security Rules หรือสร้างต้นแบบการออกแบบ UI การลงชื่อเข้าใช้ การทำงานในเครื่องโดยไม่ต้องติดตั้งใช้งานบริการจริงอาจเป็นไอเดียที่ดี
Authenticationโปรแกรมจำลองเป็นส่วนหนึ่งของ Local Emulator Suite ซึ่ง
ช่วยให้แอปของคุณโต้ตอบกับเนื้อหาและการกำหนดค่าฐานข้อมูลจำลองได้
รวมถึงทรัพยากรโปรเจ็กต์จำลอง (ฟังก์ชัน ฐานข้อมูลอื่นๆ
และกฎการรักษาความปลอดภัย) ด้วย
การใช้โปรแกรมจำลอง Authentication มีขั้นตอนเพียงไม่กี่ขั้นตอนดังนี้
- การเพิ่มบรรทัดโค้ดลงในการกำหนดค่าการทดสอบของแอปเพื่อเชื่อมต่อกับโปรแกรมจำลอง
- จากรูทของไดเรกทอรีโปรเจ็กต์ในเครื่อง ให้เรียกใช้
firebase emulators:start
- ใช้ Local Emulator Suite UI สำหรับการสร้างต้นแบบแบบอินเทอร์แอกทีฟ หรือใช้
Authentication REST API ของโปรแกรมจำลองสำหรับการทดสอบแบบไม่โต้ตอบ
ดูคำแนะนำแบบละเอียดได้ที่เชื่อมต่อแอปกับโปรแกรมจำลอง Authentication
ดูข้อมูลเพิ่มเติมได้ที่Local Emulator Suiteบทนำ
ตอนนี้เรามาดูวิธีตรวจสอบสิทธิ์ผู้ใช้กันต่อ
ลงชื่อสมัครใช้ผู้ใช้ใหม่
สร้างแบบฟอร์มที่อนุญาตให้ผู้ใช้ใหม่ลงทะเบียนกับแอปของคุณโดยใช้อีเมล
และรหัสผ่าน เมื่อผู้ใช้กรอกแบบฟอร์มเสร็จแล้ว ให้ตรวจสอบอีเมล
และรหัสผ่านที่ผู้ใช้ระบุ จากนั้นส่งไปยังเมธอด createUserWithEmailAndPassword
Web
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed up
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
Web
firebase.auth().createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
// ..
});
ลงชื่อเข้าใช้ผู้ใช้ที่มีอยู่
สร้างแบบฟอร์มที่อนุญาตให้ผู้ใช้เดิมลงชื่อเข้าใช้ด้วยอีเมล
และรหัสผ่านของตน เมื่อผู้ใช้กรอกแบบฟอร์มเสร็จสมบูรณ์ ให้เรียกใช้เมธอด signInWithEmailAndPassword
ดังนี้
Web
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
Web
firebase.auth().signInWithEmailAndPassword(email, password)
.then((userCredential) => {
// Signed in
var user = userCredential.user;
// ...
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});
ตั้งค่าเครื่องสังเกตสถานะการตรวจสอบสิทธิ์และรับข้อมูลผู้ใช้
สำหรับหน้าของแอปแต่ละหน้าที่ต้องการข้อมูลเกี่ยวกับผู้ใช้ที่ลงชื่อเข้าใช้
ให้แนบ Observer ไปยังออบเจ็กต์การตรวจสอบสิทธิ์ส่วนกลาง ระบบจะเรียกใช้ Observer นี้
ทุกครั้งที่สถานะการลงชื่อเข้าใช้ของผู้ใช้เปลี่ยนแปลง
แนบ Observer โดยใช้วิธี onAuthStateChanged
เมื่อผู้ใช้ลงชื่อเข้าใช้สำเร็จ คุณจะดูข้อมูลเกี่ยวกับผู้ใช้ได้ใน Observer
Web
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = 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.user
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
Web
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.User
var uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
ขั้นตอนถัดไป
ดูวิธีเพิ่มการรองรับผู้ให้บริการข้อมูลประจำตัวอื่นๆ และบัญชีผู้ใช้ชั่วคราวที่ไม่ระบุชื่อ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-23 UTC
[null,null,["อัปเดตล่าสุด 2025-08-23 UTC"],[],[],null,["# Get Started with Firebase Authentication on Websites\n\nYou 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-----------------------------------------\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\n### Web\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\n### Web\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----------------------------------------------------------------\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-----------------\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\n### Web\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\n### Web\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----------------------\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\n### Web\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\n### Web\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------------------------------------------------------\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\n### Web\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\n### Web\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----------\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)"]]