با احراز هویت 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 emulator 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;
});
یک ناظر وضعیت احراز هویت تنظیم کنید و داده های کاربر را دریافت کنید
برای هر یک از صفحات برنامه شما که به اطلاعاتی در مورد کاربر واردشده به سیستم نیاز دارند، یک ناظر را به شی احراز هویت جهانی پیوست کنید. هر زمان که وضعیت ورود کاربر تغییر کند، این ناظر فراخوانی می شود.
مشاهده گر را با استفاده از روش onAuthStateChanged
وصل کنید. هنگامی که یک کاربر با موفقیت وارد سیستم می شود، می توانید اطلاعاتی در مورد کاربر در مشاهده کننده دریافت کنید.
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
// ...
}
});
مراحل بعدی
با نحوه افزودن پشتیبانی برای سایر ارائه دهندگان هویت و حساب های مهمان ناشناس آشنا شوید:
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-22 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-22 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]