إدارة المستخدمين في Firebase

إنشاء مستخدم

يمكنك إنشاء مستخدِم جديد في مشروع Firebase من خلال استدعاء createUserWithEmailAndPassword أو من خلال تسجيل دخول المستخدم لأول مرة باستخدام هوية موحدة مثل تسجيل الدخول باستخدام حساب Google تسجيل الدخول إلى Facebook.

يمكنك أيضًا إنشاء مستخدمين جدد تمت مصادقتهم بكلمة المرور من خلال صفحة في وحدة تحكم Firebase أو في صفحة "المستخدمون" أو من خلال استخدام SDK للمشرف.

الحصول على المستخدم المُسجِّل الدخول حاليًا

الطريقة الموصى بها للحصول على المستخدم الحالي هي تعيين مراقب على عنصر المصادقة:

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
    // ...
  }
});

بالاستعانة بمراقب، فإنك تضمن أن كائن Auth ليس في وسيط الحالة - مثل التهيئة - عندما تحصل على المستخدم الحالي. عندما تريد استخدام 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, "[email protected]").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web

const user = firebase.auth().currentUser;

user.updateEmail("[email protected]").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 لإعادة التوجيه إلى التطبيق عند إرسال رسالة تحقق.

بالإضافة إلى ذلك، يمكنك أقلمة رسالة إثبات الملكية من خلال تعديل اللغة. الرمز على مثيل Auth قبل إرسال الرسالة الإلكترونية. على سبيل المثال:

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 لإعادة التوجيه إلى التطبيق عند إرسال رسالة إلكترونية لإعادة تعيين كلمة المرور.

بالإضافة إلى ذلك، يمكنك ترجمة الرسالة الإلكترونية لإعادة ضبط كلمة المرور من خلال تعديل اللغة. الرمز على مثيل Auth قبل إرسال الرسالة الإلكترونية. على سبيل المثال:

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