Firebase में उपयोगकर्ताओं को मैनेज करें

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

अपने Firebase प्रोजेक्ट में कोई नया उपयोगकर्ता बनाने के लिए, createUserWithEmailAndPassword तरीके को कॉल करें या Google साइन-इन या Facebook लॉगिन जैसे फ़ेडरेटेड आइडेंटिटी प्रोवाइडर का इस्तेमाल करके, उपयोगकर्ता को पहली बार साइन इन करें.

Firebase कंसोल के 'पुष्टि करना' सेक्शन में, उपयोगकर्ता पेज पर या एडमिन SDK का इस्तेमाल करके भी, पासवर्ड से पुष्टि किए गए नए उपयोगकर्ता बनाए जा सकते हैं.

वर्तमान में प्रवेश किए हुए उपयोगकर्ता को पाएं

मौजूदा उपयोगकर्ता हासिल करने का सुझाया गया तरीका, ऑथराइज़ेशन ऑब्जेक्ट पर ऑब्ज़र्वर सेट करना है:

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
    // ...
  }
});

ऑब्ज़र्वर का इस्तेमाल करके, आप यह पक्का करते हैं कि जब आपको मौजूदा उपयोगकर्ता मिल जाए, तब आप यह पक्का करें कि अनुमति ऑब्जेक्ट बीच में न हो. जैसे, इनिशलाइज़ेशन. जब 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 सहायता केंद्र में ईमेल टेंप्लेट देखें.

पुष्टि करने के लिए ईमेल भेजते समय, ऐप्लिकेशन पर वापस रीडायरेक्ट करने के लिए, यूआरएल जारी रखें विकल्प का इस्तेमाल करके, स्थिति को पास किया जा सकता है.

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

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 सहायता केंद्र में ईमेल टेंप्लेट देखें.

पासवर्ड रीसेट ईमेल भेजते समय ऐप्लिकेशन पर वापस रीडायरेक्ट करने के लिए, यूआरएल जारी रखें के ज़रिए राज्य को पास करना भी मुमकिन है.

इसके अलावा, ईमेल भेजने से पहले, पुष्टि वाले इंस्टेंस पर भाषा कोड को अपडेट करके, पासवर्ड फिर से सेट करने की सुविधा वाले ईमेल को स्थानीय भाषा में बदला जा सकता है. उदाहरण के लिए:

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 प्रोजेक्ट में उपयोगकर्ता खातों को इंपोर्ट किया जा सकता है. उदाहरण के लिए:

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