إنشاء مستخدم
يمكنك إنشاء مستخدم جديد في مشروع Firebase من خلال استدعاء الطريقة
createUserWithEmailAndPassword
أو عن طريق تسجيل دخول المستخدم لأول مرة باستخدام موفِّر هوية
موحدة، مثل تسجيل الدخول بحساب Google أو
تسجيل الدخول إلى Facebook.
يمكنك أيضًا إنشاء مستخدمين جُدد تمت مصادقتهم بكلمة المرور من قسم "المصادقة" في وحدة تحكُّم Firebase أو من صفحة "المستخدمون" أو باستخدام SDK للمشرف.
الحصول على المستخدم المُسجِّل الدخول حاليًا
الطريقة المُقترَحة للحصول على المستخدم الحالي هي ضبط مراقب على كائن Auth:
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 // ... } });
باستخدام أداة الرصد، يمكنك التأكّد من أنّ عنصر المصادقة ليس في حالة متوسطة، مثل الإعداد، عندما تحصل على المستخدم الحالي. عند استخدام signInWithRedirect
، ينتظر مراقب onAuthStateChanged
حتى
يتم حل getRedirectResult
قبل التفعيل.
يمكنك أيضًا معرفة المستخدم المسجَّل الدخول إليه حاليًا باستخدام السمة currentUser
. إذا لم يسجّل مستخدم الدخول، لا يُمثل currentUser
أي قيمة:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; if (user) { // User is signed in, see docs for a list of available properties // https://firebase.google.com/docs/reference/js/auth.user // ... } else { // No user is signed in. }
Web
const user = firebase.auth().currentUser; if (user) { // User is signed in, see docs for a list of available properties // https://firebase.google.com/docs/reference/js/v8/firebase.User // ... } else { // No user is signed in. }
الحصول على ملف شخصي للمستخدم
للحصول على معلومات الملف الشخصي لمستخدم، استخدِم خصائص مثيل User
. على سبيل المثال:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; if (user !== null) { // The user object has basic properties such as display name, email, etc. const displayName = user.displayName; const email = user.email; const photoURL = user.photoURL; const emailVerified = user.emailVerified; // The user's ID, unique to the Firebase project. Do NOT use // this value to authenticate with your backend server, if // you have one. Use User.getToken() instead. const uid = user.uid; }
Web
const user = firebase.auth().currentUser; if (user !== null) { // The user object has basic properties such as display name, email, etc. const displayName = user.displayName; const email = user.email; const photoURL = user.photoURL; const emailVerified = user.emailVerified; // The user's ID, unique to the Firebase project. Do NOT use // this value to authenticate with your backend server, if // you have one. Use User.getIdToken() instead. const uid = user.uid; }
الحصول على معلومات الملف الشخصي الخاصة بمقدّم خدمة المستخدِم
لاسترداد معلومات الملف الشخصي من مقدّمي الخدمات الذين يمكنهم تسجيل الدخول والمرتبطين بأحد المستخدمين، استخدِم السمة providerData
. على سبيل المثال:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; if (user !== null) { user.providerData.forEach((profile) => { console.log("Sign-in provider: " + profile.providerId); console.log(" Provider-specific UID: " + profile.uid); console.log(" Name: " + profile.displayName); console.log(" Email: " + profile.email); console.log(" Photo URL: " + profile.photoURL); }); }
Web
const user = firebase.auth().currentUser; if (user !== null) { user.providerData.forEach((profile) => { console.log("Sign-in provider: " + profile.providerId); console.log(" Provider-specific UID: " + profile.uid); console.log(" Name: " + profile.displayName); console.log(" Email: " + profile.email); console.log(" Photo URL: " + profile.photoURL); }); }
تعديل الملف الشخصي للمستخدم
يمكنك تعديل معلومات الملف الشخصي الأساسية للمستخدم، مثل الاسم المعروض
وعنوان URL لصورة الملف الشخصي، وذلك باستخدام الطريقة updateProfile
. على سبيل المثال:
Web
import { getAuth, updateProfile } from "firebase/auth"; const auth = getAuth(); updateProfile(auth.currentUser, { displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(() => { // Profile updated! // ... }).catch((error) => { // An error occurred // ... });
Web
const user = firebase.auth().currentUser; user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(() => { // Update successful // ... }).catch((error) => { // An error occurred // ... });
ضبط عنوان البريد الإلكتروني للمستخدم
يمكنك ضبط عنوان البريد الإلكتروني للمستخدم باستخدام الطريقة updateEmail
. على سبيل المثال:
Web
import { getAuth, updateEmail } from "firebase/auth"; const auth = getAuth(); updateEmail(auth.currentUser, "user@example.com").then(() => { // Email updated! // ... }).catch((error) => { // An error occurred // ... });
Web
const user = firebase.auth().currentUser; user.updateEmail("user@example.com").then(() => { // Update successful // ... }).catch((error) => { // An error occurred // ... });
إرسال رسالة تحقق إلى المستخدم
يمكنك إرسال رسالة إلكترونية لإثبات صحة العنوان إلى مستخدم باستخدام
الطريقة sendEmailVerification
. على سبيل المثال:
Web
import { getAuth, sendEmailVerification } from "firebase/auth"; const auth = getAuth(); sendEmailVerification(auth.currentUser) .then(() => { // Email verification sent! // ... });
Web
firebase.auth().currentUser.sendEmailVerification() .then(() => { // Email verification sent! // ... });
يمكنك تخصيص نموذج البريد الإلكتروني المُستخدَم في قسم "المصادقة" ضمن وحدة تحكُّم Firebase، في صفحة "نماذج البريد الإلكتروني". اطّلِع على نماذج الرسائل الإلكترونية في مركز مساعدة Firebase.
من الممكن أيضًا تمرير الحالة عبر عنوان URL للمتابعة لإعادة التوجيه إلى التطبيق عند إرسال رسالة تحقق.
بالإضافة إلى ذلك، يمكنك أقلمة رسالة التأكيد الإلكترونية عن طريق تعديل رمز اللغة على مثيل المصادقة قبل إرسال الرسالة الإلكترونية. على سبيل المثال:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // auth.useDeviceLanguage();
Web
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
ضبط كلمة مرور مستخدم
يمكنك ضبط كلمة مرور المستخدم باستخدام الطريقة updatePassword
. على سبيل المثال:
Web
import { getAuth, updatePassword } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; const newPassword = getASecureRandomPassword(); updatePassword(user, newPassword).then(() => { // Update successful. }).catch((error) => { // An error ocurred // ... });
Web
const user = firebase.auth().currentUser; const newPassword = getASecureRandomPassword(); user.updatePassword(newPassword).then(() => { // Update successful. }).catch((error) => { // An error ocurred // ... });
إرسال رسالة إلكترونية لإعادة ضبط كلمة المرور
يمكنك إرسال رسالة إلكترونية لإعادة ضبط كلمة المرور إلى مستخدم باتّباع الطريقة sendPasswordResetEmail
. على سبيل المثال:
Web
import { getAuth, sendPasswordResetEmail } from "firebase/auth"; const auth = getAuth(); sendPasswordResetEmail(auth, email) .then(() => { // Password reset email sent! // .. }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; // .. });
Web
firebase.auth().sendPasswordResetEmail(email) .then(() => { // Password reset email sent! // .. }) .catch((error) => { var errorCode = error.code; var errorMessage = error.message; // .. });
يمكنك تخصيص نموذج البريد الإلكتروني المُستخدَم في قسم "المصادقة" ضمن وحدة تحكُّم Firebase، في صفحة "نماذج البريد الإلكتروني". اطّلِع على نماذج الرسائل الإلكترونية في مركز مساعدة Firebase.
من الممكن أيضًا تمرير الحالة عبر عنوان URL للمتابعة لإعادة التوجيه إلى التطبيق عند إرسال رسالة إلكترونية لإعادة ضبط كلمة المرور.
بالإضافة إلى ذلك، يمكنك ترجمة الرسالة الإلكترونية لإعادة ضبط كلمة المرور عن طريق تعديل رمز اللغة على مثيل المصادقة قبل إرسال الرسالة الإلكترونية. على سبيل المثال:
Web
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // auth.useDeviceLanguage();
Web
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
يمكنك أيضًا إرسال رسائل إلكترونية لإعادة ضبط كلمة المرور من "وحدة تحكُّم Firebase".
حذف مستخدم
يمكنك حذف حساب مستخدم من خلال الطريقة delete
. على سبيل المثال:
Web
import { getAuth, deleteUser } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; deleteUser(user).then(() => { // User deleted. }).catch((error) => { // An error ocurred // ... });
Web
const user = firebase.auth().currentUser; user.delete().then(() => { // User deleted. }).catch((error) => { // An error ocurred // ... });
يمكنك أيضًا حذف المستخدمين من قسم "المصادقة" في وحدة تحكُّم Firebase في صفحة "المستخدمون".
إعادة مصادقة مستخدم
تتطلب بعض الإجراءات الحسّاسة المرتبطة بالأمان، مثل
حذف حساب
وضبط عنوان بريد إلكتروني رئيسي
وتغيير كلمة المرور، أن يكون المستخدم قد سجّل
الدخول مؤخرًا. إذا نفّذت أحد هذه الإجراءات وسجّل المستخدم دخوله منذ وقت طويل جدًا، سيتعذّر تنفيذ الإجراء مع ظهور خطأ.
عند حدوث ذلك، عليك إعادة مصادقة المستخدم من خلال الحصول على بيانات اعتماد تسجيل دخول جديدة
من المستخدم وتمرير بيانات الاعتماد إلى reauthenticateWithCredential
.
على سبيل المثال:
Web
import { getAuth, reauthenticateWithCredential } from "firebase/auth"; const auth = getAuth(); const user = auth.currentUser; // TODO(you): prompt the user to re-provide their sign-in credentials const credential = promptForCredentials(); reauthenticateWithCredential(user, credential).then(() => { // User re-authenticated. }).catch((error) => { // An error ocurred // ... });
Web
const user = firebase.auth().currentUser; // TODO(you): prompt the user to re-provide their sign-in credentials const credential = promptForCredentials(); user.reauthenticateWithCredential(credential).then(() => { // User re-authenticated. }).catch((error) => { // An error occurred // ... });
استيراد حسابات المستخدمين
يمكنك استيراد حسابات المستخدمين من ملف إلى مشروع Firebase باستخدام الأمر auth:import
في واجهة سطر الأوامر في Firebase. على سبيل المثال:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14