जावास्क्रिप्ट का उपयोग करके एकाधिक प्रामाणिक प्रदाताओं को एक खाते से लिंक करें

आप किसी मौजूदा उपयोगकर्ता खाते से प्रमाणीकरण प्रदाता क्रेडेंशियल्स को लिंक करके उपयोगकर्ताओं को एकाधिक प्रमाणीकरण प्रदाताओं का उपयोग करके अपने ऐप में साइन इन करने की अनुमति दे सकते हैं। उपयोगकर्ताओं को उसी फायरबेस उपयोगकर्ता आईडी से पहचाना जा सकता है, चाहे वे साइन इन करने के लिए किसी भी प्रमाणीकरण प्रदाता का उपयोग करें। उदाहरण के लिए, पासवर्ड से साइन इन करने वाला उपयोगकर्ता Google खाता लिंक कर सकता है और भविष्य में किसी भी विधि से साइन इन कर सकता है। या, कोई अनाम उपयोगकर्ता फेसबुक अकाउंट लिंक कर सकता है और फिर, बाद में, आपके ऐप का उपयोग जारी रखने के लिए फेसबुक से साइन इन कर सकता है।

शुरू करने से पहले

अपने ऐप में दो या अधिक प्रमाणीकरण प्रदाताओं (संभवतः अनाम प्रमाणीकरण सहित) के लिए समर्थन जोड़ें।

Google या Facebook जैसे प्रमाणीकरण प्रदाता के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करने के लिए:

  1. किसी भी प्रमाणीकरण प्रदाता या विधि का उपयोग करके उपयोगकर्ता में साइन इन करें।
  2. AuthProvider ऑब्जेक्ट प्राप्त करें जो उस प्रदाता से मेल खाता है जिसे आप उपयोगकर्ता के खाते से लिंक करना चाहते हैं। उदाहरण:

    Web modular API

    import { GoogleAuthProvider, FacebookAuthProvider, TwitterAuthProvider, GithubAuthProvider } from "firebase/auth";
    
    const googleProvider = new GoogleAuthProvider();
    const facebookProvider = new FacebookAuthProvider();
    const twitterProvider = new TwitterAuthProvider();
    const githubProvider = new GithubAuthProvider();

    Web namespaced API

    var googleProvider = new firebase.auth.GoogleAuthProvider();
    var facebookProvider = new firebase.auth.FacebookAuthProvider();
    var twitterProvider = new firebase.auth.TwitterAuthProvider();
    var githubProvider = new firebase.auth.GithubAuthProvider();
  3. उपयोगकर्ता को उस प्रदाता के साथ साइन इन करने के लिए प्रेरित करें जिसे आप लिंक करना चाहते हैं। आप अपने उपयोगकर्ताओं को पॉप-अप विंडो खोलकर या प्रदाता के साइन-इन पृष्ठ पर रीडायरेक्ट करके साइन इन करने के लिए संकेत दे सकते हैं। मोबाइल उपकरणों पर रीडायरेक्ट विधि को प्राथमिकता दी जाती है।
    • पॉप-अप विंडो से साइन इन करने के लिए, linkWithPopup पर कॉल करें:

      Web modular API

      import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth";
      const provider = new GoogleAuthProvider();
      
      const auth = getAuth();
      linkWithPopup(auth.currentUser, provider).then((result) => {
        // Accounts successfully linked.
        const credential = GoogleAuthProvider.credentialFromResult(result);
        const user = result.user;
        // ...
      }).catch((error) => {
        // Handle Errors here.
        // ...
      });

      Web namespaced API

      auth.currentUser.linkWithPopup(provider).then((result) => {
        // Accounts successfully linked.
        var credential = result.credential;
        var user = result.user;
        // ...
      }).catch((error) => {
        // Handle Errors here.
        // ...
      });
    • प्रदाता के साइन-इन पेज पर रीडायरेक्ट करके साइन इन करने के लिए, linkWithRedirect पर कॉल करें: `linkWithRedirect` का उपयोग करते समय सर्वोत्तम प्रथाओं का पालन करें।

      Web modular API

      import { getAuth, linkWithRedirect, GoogleAuthProvider } from "firebase/auth";
      const provider = new GoogleAuthProvider();
      
      const auth = getAuth();
      linkWithRedirect(auth.currentUser, provider)
        .then(/* ... */)
        .catch(/* ... */);

      Web namespaced API

      auth.currentUser.linkWithRedirect(provider)
        .then(/* ... */)
        .catch(/* ... */);
      उपयोगकर्ता द्वारा साइन इन करने के बाद, उन्हें आपके पृष्ठ पर वापस भेज दिया जाता है। फिर, जब आपका पृष्ठ लोड होता है तो आप getRedirectResult पर कॉल करके साइन-इन परिणाम पुनः प्राप्त कर सकते हैं:

      Web modular API

      import { getRedirectResult } from "firebase/auth";
      getRedirectResult(auth).then((result) => {
        const credential = GoogleAuthProvider.credentialFromResult(result);
        if (credential) {
          // Accounts successfully linked.
          const user = result.user;
          // ...
        }
      }).catch((error) => {
        // Handle Errors here.
        // ...
      });

      Web namespaced API

      auth.getRedirectResult().then((result) => {
        if (result.credential) {
          // Accounts successfully linked.
          var credential = result.credential;
          var user = result.user;
          // ...
        }
      }).catch((error) => {
        // Handle Errors here.
        // ...
      });
    यदि उपयोगकर्ता सफलतापूर्वक साइन इन है, तो प्रदाता के साथ उपयोगकर्ता का खाता आपके फायरबेस प्रोजेक्ट में उपयोगकर्ता के खाते से लिंक हो जाता है।

    यदि क्रेडेंशियल पहले से ही किसी अन्य उपयोगकर्ता खाते से लिंक हैं तो खाता लिंकिंग विफल हो जाएगी। इस स्थिति में, आपको अपने ऐप के लिए उपयुक्त खातों और संबंधित डेटा को मर्ज करना होगा:

    Web modular API

    import { getAuth, signInWithCredential, linkWithCredential, OAuthProvider } from "firebase/auth";
    
    // The implementation of how you store your user data depends on your application
    const repo = new MyUserDataRepo();
    
    // Get reference to the currently signed-in user
    const auth = getAuth();
    const prevUser = auth.currentUser;
    
    // Get the data which you will want to merge. This should be done now
    // while the app is still signed in as this user.
    const prevUserData = repo.get(prevUser);
    
    // Delete the user's data now, we will restore it if the merge fails
    repo.delete(prevUser);
    
    // Sign in user with the account you want to link to
    signInWithCredential(auth, newCredential).then((result) => {
      console.log("Sign In Success", result);
      const currentUser = result.user;
      const currentUserData = repo.get(currentUser);
    
      // Merge prevUser and currentUser data stored in Firebase.
      // Note: How you handle this is specific to your application
      const mergedData = repo.merge(prevUserData, currentUserData);
    
      const credential = OAuthProvider.credentialFromResult(result);
      return linkWithCredential(prevUser, credential)
        .then((linkResult) => {
          // Sign in with the newly linked credential
          const linkCredential = OAuthProvider.credentialFromResult(linkResult);
          return signInWithCredential(auth, linkCredential);
        })
        .then((signInResult) => {
          // Save the merged data to the new user
          repo.set(signInResult.user, mergedData);
        });
    }).catch((error) => {
      // If there are errors we want to undo the data merge/deletion
      console.log("Sign In Error", error);
      repo.set(prevUser, prevUserData);
    });

    Web namespaced API

    // The implementation of how you store your user data depends on your application
    var repo = new MyUserDataRepo();
    
    // Get reference to the currently signed-in user
    var prevUser = auth.currentUser;
    
    // Get the data which you will want to merge. This should be done now
    // while the app is still signed in as this user.
    var prevUserData = repo.get(prevUser);
    
    // Delete the user's data now, we will restore it if the merge fails
    repo.delete(prevUser);
    
    // Sign in user with the account you want to link to
    auth.signInWithCredential(newCredential).then((result) => {
      console.log("Sign In Success", result);
      var currentUser = result.user;
      var currentUserData = repo.get(currentUser);
    
      // Merge prevUser and currentUser data stored in Firebase.
      // Note: How you handle this is specific to your application
      var mergedData = repo.merge(prevUserData, currentUserData);
    
      return prevUser.linkWithCredential(result.credential)
        .then((linkResult) => {
          // Sign in with the newly linked credential
          return auth.signInWithCredential(linkResult.credential);
        })
        .then((signInResult) => {
          // Save the merged data to the new user
          repo.set(signInResult.user, mergedData);
        });
    }).catch((error) => {
      // If there are errors we want to undo the data merge/deletion
      console.log("Sign In Error", error);
      repo.set(prevUser, prevUserData);
    });

किसी मौजूदा उपयोगकर्ता खाते में ईमेल पता और पासवर्ड क्रेडेंशियल जोड़ने के लिए:

  1. किसी भी प्रमाणीकरण प्रदाता या विधि का उपयोग करके उपयोगकर्ता में साइन इन करें।
  2. उपयोगकर्ता को ईमेल पता और नए पासवर्ड के लिए संकेत दें।
  3. ईमेल पते और पासवर्ड के साथ एक AuthCredential ऑब्जेक्ट बनाएं:

    Web modular API

    import { EmailAuthProvider } from "firebase/auth";
    
    const credential = EmailAuthProvider.credential(email, password);

    Web namespaced API

    var credential = firebase.auth.EmailAuthProvider.credential(email, password);
  4. AuthCredential ऑब्जेक्ट को साइन-इन किए गए उपयोगकर्ता के linkWithCredential विधि से पास करें:

    Web modular API

    import { getAuth, linkWithCredential } from "firebase/auth";
    
    const auth = getAuth();
    linkWithCredential(auth.currentUser, credential)
      .then((usercred) => {
        const user = usercred.user;
        console.log("Account linking success", user);
      }).catch((error) => {
        console.log("Account linking error", error);
      });

    Web namespaced API

    auth.currentUser.linkWithCredential(credential)
      .then((usercred) => {
        var user = usercred.user;
        console.log("Account linking success", user);
      }).catch((error) => {
        console.log("Account linking error", error);
      });

    यदि क्रेडेंशियल पहले से ही किसी अन्य उपयोगकर्ता खाते से लिंक हैं तो linkWithCredential पर कॉल विफल हो जाएगी। इस स्थिति में, आपको अपने ऐप के लिए उपयुक्त खातों और संबंधित डेटा को मर्ज करना होगा (ऊपर उदाहरण देखें)।

आप किसी प्रमाणीकरण प्रदाता को किसी खाते से अनलिंक कर सकते हैं, ताकि उपयोगकर्ता अब उस प्रदाता के साथ साइन इन न कर सके।

किसी उपयोगकर्ता खाते से प्रमाणीकरण प्रदाता को अनलिंक करने के लिए, प्रदाता आईडी को unlink विधि में पास करें। आप providerData प्रॉपर्टी से किसी उपयोगकर्ता से जुड़े प्रामाणिक प्रदाताओं की प्रदाता आईडी प्राप्त कर सकते हैं।

Web modular API

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

const auth = getAuth();
unlink(auth.currentUser, providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});

Web namespaced API

user.unlink(providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});