ایجاد کاربر
برای ایجاد کاربر جدید، گزینههای زیر را دارید:
از برنامهتان: با فراخوانی روش
createUserWithEmailAndPasswordیا با ورود به سیستم کاربر برای اولینبار بااستفاده از ارائهدهنده هویت فدراسیونی، مانند ورود به سیستم با Google یا ورود به سیستم Facebook، کاربر جدیدی در پروژه Firebase خود ایجاد کنید.در کنسول 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; // 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 ببینید.
همچنین میتوان وضعیت را ازطریق نشانی وب ادامه ارسال کرد تا هنگام ارسال ایمیل بازنشانی گذرواژه، به برنامه هدایت شود.
علاوهبراین، میتوانید با بهروزرسانی کد زبان در نمونه 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