إدارة المستخدمين في 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
    // ...
  }
});

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

بالإضافة إلى ذلك، يمكنك توفير رسالة التحقّق الإلكترونية بلغة أخرى من خلال تعديل رمز اللغة في مثيل 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 CLI. على سبيل المثال:

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