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

إنشاء مستخدم

تتوفّر لك الخيارات التالية لإنشاء مستخدم جديد:

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

الطريقة المقترَحة للحصول على المستخدم الحالي هي من خلال ضبط مراقِب على كائن 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;
// Add your own function here to read the user's entered password, or generate a new one
const newPassword = getASecureRandomPassword();

updatePassword(user, newPassword).then(() => {
  // Update successful.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web

const user = firebase.auth().currentUser;
// Add your own function here to read the user's entered password, or generate a new one
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 CLI. على سبيل المثال:

firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14