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

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

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

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

मौजूदा साइन इन उपयोगकर्ता की जानकारी पाना

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