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

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

नया उपयोगकर्ता बनाने के लिए, आपके पास ये विकल्प हैं:

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

  • 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 console में जाकर भी उपयोगकर्ताओं को मिटाया जा सकता है. इसके लिए, सुरक्षा > पुष्टि > उपयोगकर्ता टैब पर जाएं.

किसी उपयोगकर्ता की फिर से पुष्टि करना

सुरक्षा से जुड़ी कुछ संवेदनशील कार्रवाइयों के लिए, उपयोगकर्ता का हाल ही में साइन इन करना ज़रूरी है. जैसे, खाता मिटाना, मुख्य ईमेल पता सेट करना, और पासवर्ड बदलना. इनमें से कोई कार्रवाई करने पर, अगर उपयोगकर्ता ने बहुत पहले साइन इन किया था, तो गड़बड़ी की वजह से कार्रवाई पूरी नहीं होगी. ऐसा होने पर, उपयोगकर्ता से साइन इन करने के नए क्रेडेंशियल लेकर उसकी फिर से पुष्टि करें. इसके बाद, उन क्रेडेंशियल को 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 CLI की auth:import कमांड का इस्तेमाल करके, किसी फ़ाइल से उपयोगकर्ता खातों को अपने Firebase प्रोजेक्ट में इंपोर्ट किया जा सकता है. उदाहरण के लिए:

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