مدیریت کاربران در 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);
  });
}

به‌روزرسانی نمایه کاربر

می‌توانید اطلاعات نمایه پایه کاربر—نام نمایشی کاربر و نشانی وب عکس نمایه—را با روش 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 ببینید.

همچنین می‌توان وضعیت را ازطریق نشانی وب ادامه منتقل کرد تا هنگام ارسال ایمیل درستی‌سنجی، به برنامه هدایت شود.

علاوه‌براین، می‌توانید ایمیل درستی‌سنجی را با به‌روزرسانی کد زبان در نمونه 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 ببینید.

همچنین می‌توانید وضعیت را ازطریق نشانی وب ادامه منتقل کنید تا هنگام ارسال ایمیل بازنشانی گذرواژه، به برنامه هدایت شوید.

علاوه‌براین، می‌توانید ایمیل بازنشانی گذرواژه را با به‌روزرسانی کد زبان در نمونه 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
  // ...
});

وارد کردن حساب‌های کاربری

بااستفاده از فرمان auth:import «میانای خط فرمان Firebase»، می‌توانید حساب‌های کاربری را از فایل به پروژه Firebase خود وارد کنید. برای مثال:

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