ایجاد یک کاربر
برای ایجاد کاربر جدید، گزینههای زیر را دارید:
از برنامه شما : با فراخوانی متد
createUserWithEmailAndPasswordیا با ورود کاربر برای اولین بار با استفاده از یک ارائه دهنده هویت فدرال، مانند Google Sign-In یا Facebook Login ، یک کاربر جدید در پروژه Firebase خود ایجاد کنید.در کنسول Firebase : یک کاربر جدید با رمز عبور احراز هویت شده در تب Security > Authentication > Users ایجاد کنید.
دریافت کاربر فعلی وارد شده
روش پیشنهادی برای دریافت کاربر فعلی، تنظیم یک ناظر (observer) روی شیء 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 تهی (null) میشود:
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! // ... });
شما میتوانید قالب ایمیل مورد استفاده را در تب Security > Authentication > Templates در کنسول 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; // .. });
شما میتوانید قالب ایمیل مورد استفاده را در تب Security > Authentication > Templates در کنسول 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 در تب Security > Authentication > Users حذف کنید.
احراز هویت مجدد کاربر
برخی از اقدامات حساس به امنیت - مانند حذف حساب کاربری ، تنظیم آدرس ایمیل اصلی و تغییر رمز عبور - مستلزم آن است که کاربر اخیراً وارد سیستم شده باشد. اگر یکی از این اقدامات را انجام دهید و کاربر مدت زیادی از ورود او گذشته باشد، اقدام با خطا مواجه میشود. در این صورت، با دریافت اعتبارنامههای ورود جدید از کاربر و ارسال اعتبارنامهها به 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 CLI، حسابهای کاربری را از یک فایل به پروژه Firebase خود وارد کنید. برای مثال:
firebase auth:import users.json --hash-algo=scrypt --rounds=8 --mem-cost=14