पुष्टि करने वाली सेवा देने वाली कंपनी के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करके, उपयोगकर्ताओं को पुष्टि करने वाली कई सेवाओं का इस्तेमाल करके, आपके ऐप्लिकेशन में साइन इन करने की अनुमति दी जा सकती है. उपयोगकर्ताओं की पहचान, एक ही Firebase यूज़र आईडी से की जा सकती है. भले ही, उन्होंने साइन इन करने के लिए पुष्टि करने वाली सेवा देने वाली किसी भी कंपनी का इस्तेमाल किया हो. उदाहरण के लिए, पासवर्ड से साइन इन करने वाला उपयोगकर्ता, Google खाता लिंक कर सकता है. साथ ही, वह आने वाले समय में दोनों में से किसी भी तरीके से साइन इन कर सकता है. इसके अलावा, कोई ऐसा उपयोगकर्ता जो अपनी पहचान छिपाकर ऐप्लिकेशन का इस्तेमाल कर रहा है, वह बाद में Facebook खाता लिंक कर सकता है. इसके बाद, आपके ऐप्लिकेशन का इस्तेमाल जारी रखने के लिए, Facebook खाते से साइन इन कर सकता है.
शुरू करने से पहले
अपने ऐप्लिकेशन में, पुष्टि करने की सुविधा देने वाली दो या उससे ज़्यादा कंपनियों के लिए सहायता जोड़ें. इसमें, हो सकता है कि गुमनाम पुष्टि करने की सुविधा भी शामिल हो.
उपयोगकर्ता खाते से, फ़ेडरेटेड ऑथेंटिकेशन प्रोवाइडर के क्रेडेंशियल लिंक करना
पुष्टि करने वाली सेवा देने वाली कंपनी, जैसे कि Google या Facebook के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करने के लिए:
- पुष्टि करने वाली किसी भी सेवा या तरीके का इस्तेमाल करके, उपयोगकर्ता को साइन इन कराएं.
- उस सेवा देने वाली कंपनी से जुड़ा
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();
- उपयोगकर्ता को उस सेवा देने वाली कंपनी से साइन इन करने के लिए कहें जिसे आपको लिंक करना है. आपके पास अपने उपयोगकर्ताओं को साइन इन करने के लिए कहने का विकल्प होता है. इसके लिए, आपके पास पॉप-अप विंडो खोलने या सेवा देने वाली कंपनी के साइन-इन पेज पर रीडायरेक्ट करने का विकल्प होता है. मोबाइल डिवाइसों पर, रीडायरेक्ट करने का तरीका इस्तेमाल करना बेहतर होता है.
- पॉप-अप विंडो से साइन इन करने के लिए,
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. // ... });
अगर क्रेडेंशियल पहले से ही किसी दूसरे उपयोगकर्ता खाते से लिंक हैं, तो खाता लिंक नहीं किया जा सकेगा. इस स्थिति में, आपको अपने ऐप्लिकेशन के हिसाब से, खातों और उनसे जुड़े डेटा को मर्ज करने की सुविधा को मैनेज करना होगा:
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); });
- पॉप-अप विंडो से साइन इन करने के लिए,
ईमेल पता और पासवर्ड क्रेडेंशियल को उपयोगकर्ता खाते से लिंक करना
किसी मौजूदा उपयोगकर्ता खाते में ईमेल पता और पासवर्ड क्रेडेंशियल जोड़ने के लिए:
- पुष्टि करने वाली किसी भी सेवा या तरीके का इस्तेमाल करके, उपयोगकर्ता को साइन इन कराएं.
- उपयोगकर्ता से ईमेल पता और नया पासवर्ड मांगें.
- ईमेल पते और पासवर्ड की मदद से
AuthCredential
ऑब्जेक्ट बनाएं:Web
import { EmailAuthProvider } from "firebase/auth"; const credential = EmailAuthProvider.credential(email, password);
Web
var credential = firebase.auth.EmailAuthProvider.credential(email, password);
साइन इन किए हुए उपयोगकर्ता के
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 // ... });