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