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

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

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

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

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

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

Web version 9

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 version 8

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 version 9

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 version 8

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 version 9

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 version 8

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 version 9

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 version 8

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

उपयोगकर्ता की प्रोफ़ाइल अपडेट करें

आप किसी उपयोगकर्ता की मूल प्रोफ़ाइल जानकारी—उपयोगकर्ता का प्रदर्शन नाम और प्रोफ़ाइल फ़ोटो URL— updateProfile पद्धति से अपडेट कर सकते हैं। उदाहरण के लिए:

Web version 9

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 version 8

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 version 9

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

Web version 8

const user = firebase.auth().currentUser;

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

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

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

Web version 9

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

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

Web version 8

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

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

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

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

Web version 9

import { getAuth } from "firebase/auth";

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

Web version 8

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

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

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

Web version 9

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 version 8

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

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

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

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

Web version 9

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 version 8

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

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

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

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

Web version 9

import { getAuth } from "firebase/auth";

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

Web version 8

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

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

एक उपयोगकर्ता हटाएं

आप delete मेथड से यूजर अकाउंट को डिलीट कर सकते हैं। उदाहरण के लिए:

Web version 9

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

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

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

Web version 8

const user = firebase.auth().currentUser;

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

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

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

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

Web version 9

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 version 8

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