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

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

वेब कंटेनर इंस्टॉल करने से पहले

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

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

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

    वेब मॉड्यूलर एपीआई

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

    वेब नेमस्पेसेड एपीआई

    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 पर कॉल करें:

      वेब मॉड्यूलर एपीआई

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

      वेब नेमस्पेसेड एपीआई

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

      वेब मॉड्यूलर एपीआई

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

      वेब नेमस्पेसेड एपीआई

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

      वेब मॉड्यूलर एपीआई

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

      वेब नेमस्पेसेड एपीआई

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

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

    वेब मॉड्यूलर एपीआई

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

    वेब नेमस्पेसेड एपीआई

    // 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 ऑब्जेक्ट बनाएं:

    वेब मॉड्यूलर एपीआई

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

    वेब नेमस्पेसेड एपीआई

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

    वेब मॉड्यूलर एपीआई

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

    वेब नेमस्पेसेड एपीआई

    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 प्रॉपर्टी से, उपयोगकर्ता को पुष्टि करने वाली कंपनियों के आईडी मिल सकते हैं.

वेब मॉड्यूलर एपीआई

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

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

वेब नेमस्पेसेड एपीआई

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