تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك استخدام Firebase Authentication للسماح للمستخدمين بتسجيل الدخول إلى تطبيقك باستخدام طريقة واحدة أو أكثر من طرق تسجيل الدخول، بما في ذلك تسجيل الدخول باستخدام عنوان البريد الإلكتروني وكلمة المرور، وموفّري خدمات الهوية الموحّدة، مثل "تسجيل الدخول باستخدام حساب Google" و"تسجيل الدخول باستخدام حساب Facebook". يساعدك هذا البرنامج التعليمي في بدء استخدام Firebase Authentication من خلال توضيح كيفية إضافة ميزة تسجيل الدخول باستخدام عنوان البريد الإلكتروني وكلمة المرور إلى تطبيقك.
إضافة حزمة تطوير البرامج (SDK) الخاصة بـ Authentication وإعدادها
(اختياري) إنشاء نموذج أوّلي واختباره باستخدام Firebase Local Emulator Suite
قبل الحديث عن كيفية مصادقة تطبيقك للمستخدمين، دعنا نقدّم مجموعة من الأدوات التي يمكنك استخدامها لإنشاء نماذج أولية واختبار وظيفة Authentication:
Firebase Local Emulator Suite. إذا كنت بصدد الاختيار بين تقنيات وموفّري خدمات المصادقة، أو تجربة نماذج بيانات مختلفة مع بيانات عامة وخاصة باستخدام Authentication وFirebase Security Rules، أو إنشاء نماذج أولية لتصاميم واجهة مستخدم تسجيل الدخول، قد يكون من المفيد أن تتمكّن من العمل محليًا بدون نشر الخدمات المباشرة.
يُعدّ محاكي Authentication جزءًا من Local Emulator Suite، ما يتيح لتطبيقك التفاعل مع المحتوى والإعدادات المحاكية لقاعدة البيانات، بالإضافة إلى موارد مشروعك المحاكية (الدوال وقواعد البيانات الأخرى وقواعد الأمان) بشكل اختياري.
لا يتطلّب استخدام محاكي Authentication سوى بضع خطوات:
إضافة سطر من الرمز البرمجي إلى إعدادات الاختبار في تطبيقك للاتصال بالمحاكي
من جذر دليل مشروعك المحلي، شغِّل firebase emulators:start.
استخدام واجهة مستخدم Local Emulator Suite لإنشاء نماذج أولية تفاعلية، أو استخدام واجهة برمجة تطبيقات REST لمحاكي Authentication لإجراء اختبارات غير تفاعلية
أنشئ نموذجًا يتيح للمستخدمين الجدد التسجيل في تطبيقك باستخدام عنوان بريدهم الإلكتروني وكلمة مرور. عندما يكمل المستخدم النموذج، تحقَّق من صحة عنوان البريد الإلكتروني وكلمة المرور اللذين قدّمهما، ثم مرِّرهما إلى الطريقة createUserWithEmailAndPassword:
Web
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;// ..});
أنشئ نموذجًا يتيح للمستخدمين الحاليين تسجيل الدخول باستخدام عنوان بريدهم الإلكتروني وكلمة المرور. عندما يُكمل المستخدم النموذج، استدعِ الطريقة
signInWithEmailAndPassword:
Web
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;});
ضبط مراقب لحالة المصادقة والحصول على بيانات المستخدم
لكل صفحة من صفحات تطبيقك تحتاج إلى معلومات عن المستخدم الذي سجّل الدخول،
أضِف أداة مراقبة إلى عنصر المصادقة العام. يتم استدعاء هذا المراقب كلما تغيّرت حالة تسجيل الدخول الخاصة بالمستخدم.
أرفِق المراقب باستخدام طريقة onAuthStateChanged. عندما يسجّل المستخدم الدخول بنجاح، يمكنك الحصول على معلومات عنه في المراقب.
Web
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// ...}});
تاريخ التعديل الأخير: 2025-08-23 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-23 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)"]]