आप उपयोगकर्ताओं को पुष्टि करने वाली कई कंपनियों का इस्तेमाल करके, अपने ऐप्लिकेशन में साइन इन करने की अनुमति दे सकते हैं. ऐसा करने के लिए, आपको पुष्टि करने वाली कंपनी के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करना होगा. उपयोगकर्ताओं को एक ही 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);
AuthCredential
ऑब्जेक्ट को साइन इन किए हुए उपयोगकर्ता केlinkWithCredential
तरीके में पास करें: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 // ... });