JavaScript का इस्तेमाल करके, पुष्टि करने वाली एक से ज़्यादा कंपनियों को एक खाते से लिंक करना

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

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

अपने ऐप्लिकेशन में, पुष्टि करने की सेवा देने वाली दो या उससे ज़्यादा कंपनियों के लिए सहायता जोड़ें. इसमें गुमनाम तरीके से पुष्टि करने की सुविधा भी शामिल हो सकती है.

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

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

    Web

    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

    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

      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

      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

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

      Web

      auth.currentUser.linkWithRedirect(provider)
        .then(/* ... */)
        .catch(/* ... */);
      उपयोगकर्ता के साइन इन करने के बाद, उसे आपके पेज पर वापस रीडायरेक्ट कर दिया जाता है. इसके बाद, पेज लोड होने पर getRedirectResult को कॉल करके, साइन-इन करने का नतीजा वापस पाया जा सकता है:

      Web

      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

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

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

    Web

    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

    // 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

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

    Web

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

    Web

    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

    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

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

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

समस्या का हल

अगर आपको एक से ज़्यादा खाते लिंक करते समय गड़बड़ियां दिखती हैं, तो पुष्टि किए गए ईमेल पतों के बारे में जानकारी देने वाला दस्तावेज़ देखें.