फायरबेस में उपयोगकर्ताओं को प्रबंधित करें

एक उपयोगकर्ता बनाएं

आप createUserWithEmailAndPassword विधि को कॉल करके या Google साइन-इन या फेसबुक लॉगिन जैसे फ़ेडरेटेड पहचान प्रदाता का उपयोग करके पहली बार किसी उपयोगकर्ता में साइन इन करके अपने फायरबेस प्रोजेक्ट में एक नया उपयोगकर्ता बनाते हैं।

आप फायरबेस कंसोल के प्रमाणीकरण अनुभाग से, उपयोगकर्ता पृष्ठ पर, या व्यवस्थापक एसडीके का उपयोग करके नए पासवर्ड-प्रमाणीकृत उपयोगकर्ता भी बना सकते हैं।

वर्तमान में साइन-इन किया हुआ उपयोगकर्ता प्राप्त करें

वर्तमान उपयोगकर्ता को प्राप्त करने का अनुशंसित तरीका ऑथ ऑब्जेक्ट पर एक पर्यवेक्षक सेट करना है:

Web modular API

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 namespaced API

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 modular API

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 namespaced API

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 modular API

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 namespaced API

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 modular API

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 namespaced API

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 पद्धति से उपयोगकर्ता की मूल प्रोफ़ाइल जानकारी—उपयोगकर्ता का प्रदर्शन नाम और प्रोफ़ाइल फ़ोटो URL—अपडेट कर सकते हैं। उदाहरण के लिए:

Web modular API

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 namespaced API

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 modular API

import { getAuth, updateEmail } from "firebase/auth";
const auth = getAuth();
updateEmail(auth.currentUser, "user@example.com").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

Web namespaced API

const user = firebase.auth().currentUser;

user.updateEmail("user@example.com").then(() => {
  // Update successful
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

किसी उपयोगकर्ता को सत्यापन ईमेल भेजें

आप किसी उपयोगकर्ता को sendEmailVerification विधि से पता सत्यापन ईमेल भेज सकते हैं। उदाहरण के लिए:

Web modular API

import { getAuth, sendEmailVerification } from "firebase/auth";

const auth = getAuth();
sendEmailVerification(auth.currentUser)
  .then(() => {
    // Email verification sent!
    // ...
  });

Web namespaced API

firebase.auth().currentUser.sendEmailVerification()
  .then(() => {
    // Email verification sent!
    // ...
  });

आप ईमेल टेम्प्लेट पेज पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्प्लेट को कस्टमाइज़ कर सकते हैं। फायरबेस सहायता केंद्र में ईमेल टेम्पलेट देखें।

सत्यापन ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी यूआरएल के माध्यम से राज्य को पास करना भी संभव है।

इसके अतिरिक्त आप ईमेल भेजने से पहले प्रामाणिक उदाहरण पर भाषा कोड को अपडेट करके सत्यापन ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:

Web modular API

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 namespaced API

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

उपयोगकर्ता का पासवर्ड सेट करें

आप updatePassword विधि से उपयोगकर्ता का पासवर्ड सेट कर सकते हैं। उदाहरण के लिए:

Web modular API

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 namespaced API

const user = firebase.auth().currentUser;
const newPassword = getASecureRandomPassword();

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

पासवर्ड रीसेट ईमेल भेजें

आप sendPasswordResetEmail विधि से किसी उपयोगकर्ता को पासवर्ड रीसेट ईमेल भेज सकते हैं। उदाहरण के लिए:

Web modular API

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 namespaced API

firebase.auth().sendPasswordResetEmail(email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });

आप ईमेल टेम्प्लेट पेज पर फायरबेस कंसोल के प्रमाणीकरण अनुभाग में उपयोग किए जाने वाले ईमेल टेम्प्लेट को कस्टमाइज़ कर सकते हैं। फायरबेस सहायता केंद्र में ईमेल टेम्पलेट देखें।

पासवर्ड रीसेट ईमेल भेजते समय ऐप पर वापस रीडायरेक्ट करने के लिए जारी यूआरएल के माध्यम से राज्य को पास करना भी संभव है।

इसके अतिरिक्त आप ईमेल भेजने से पहले प्रामाणिक उदाहरण पर भाषा कोड को अपडेट करके पासवर्ड रीसेट ईमेल को स्थानीयकृत कर सकते हैं। उदाहरण के लिए:

Web modular API

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 namespaced API

firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();

आप फायरबेस कंसोल से पासवर्ड रीसेट ईमेल भी भेज सकते हैं।

किसी उपयोगकर्ता को हटाएँ

आप delete विधि से किसी उपयोगकर्ता खाते को हटा सकते हैं। उदाहरण के लिए:

Web modular API

import { getAuth, deleteUser } from "firebase/auth";

const auth = getAuth();
const user = auth.currentUser;

deleteUser(user).then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

Web namespaced API

const user = firebase.auth().currentUser;

user.delete().then(() => {
  // User deleted.
}).catch((error) => {
  // An error ocurred
  // ...
});

आप उपयोगकर्ता पृष्ठ पर फ़ायरबेस कंसोल के प्रमाणीकरण अनुभाग से भी उपयोगकर्ताओं को हटा सकते हैं।

किसी उपयोगकर्ता को पुनः प्रमाणित करें

कुछ सुरक्षा-संवेदनशील कार्रवाइयां—जैसे खाता हटाना , प्राथमिक ईमेल पता सेट करना और पासवर्ड बदलना —के लिए आवश्यक है कि उपयोगकर्ता ने हाल ही में साइन इन किया हो। यदि आप इनमें से कोई एक क्रिया करते हैं, और उपयोगकर्ता ने बहुत पहले साइन इन किया है, किसी त्रुटि के साथ कार्रवाई विफल हो जाती है. जब ऐसा होता है, तो उपयोगकर्ता से नए साइन-इन क्रेडेंशियल प्राप्त करके और क्रेडेंशियल्स को reauthenticateWithCredential में पास करके उपयोगकर्ता को पुनः प्रमाणित करें। उदाहरण के लिए:

Web modular API

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 namespaced API

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 CLI के auth:import कमांड का उपयोग करके किसी फ़ाइल से उपयोगकर्ता खातों को अपने Firebase प्रोजेक्ट में आयात कर सकते हैं। उदाहरण के लिए:

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