अपने ऐप्लिकेशन में Twitter की पुष्टि करने की सुविधा इंटिग्रेट करके, उपयोगकर्ताओं को अपने Twitter खातों का इस्तेमाल करके Firebase से पुष्टि करने की अनुमति दी जा सकती है. Twitter की पुष्टि करने की सुविधा को इंटिग्रेट करने के लिए, साइन-इन फ़्लो को पूरा करने के लिए Firebase SDK टूल का इस्तेमाल किया जा सकता है. इसके अलावा, Twitter OAuth फ़्लो को मैन्युअल तरीके से पूरा करके, Firebase को ऐक्सेस टोकन और सीक्रेट पास किया जा सकता है.
शुरू करने से पहले
- अपने JavaScript प्रोजेक्ट में Firebase जोड़ें.
- Firebase कंसोल में, Auth सेक्शन खोलें.
- साइन इन करने का तरीका टैब पर, Twitter की सेवा देने वाली कंपनी को चालू करें.
- सेवा देने वाली कंपनी के डेवलपर कंसोल से, एपीआई पासकोड और एपीआई पासकोड को सेवा देने वाली कंपनी के कॉन्फ़िगरेशन में जोड़ें:
- Twitter पर डेवलपर ऐप्लिकेशन के तौर पर अपने ऐप्लिकेशन को रजिस्टर करें और अपने ऐप्लिकेशन का OAuth एपीआई पासकोड और एपीआई पासवर्ड पाएं.
- पक्का करें कि आपका Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण के लिए,
my-app-12345.firebaseapp.com/__/auth/handler
), Twitter ऐप्लिकेशन के कॉन्फ़िगरेशन पर, आपके ऐप्लिकेशन के सेटिंग पेज में अनुमति वाले कॉलबैक यूआरआई के तौर पर सेट हो.
- सेव करें पर क्लिक करें.
Firebase SDK टूल की मदद से साइन इन फ़्लो को मैनेज करना
अगर कोई वेब ऐप्लिकेशन बनाया जा रहा है, तो Firebase के साथ अपने उपयोगकर्ताओं की पुष्टि करने का सबसे आसान तरीका यह है कि Firebase JavaScript SDK टूल की मदद से साइन-इन फ़्लो को मैनेज किया जाए. ऐसा करने के लिए, उपयोगकर्ताओं को अपने Twitter खाते से साइन इन करना होगा. (अगर आपको Node.js या किसी ऐसे अन्य एनवायरमेंट में उपयोगकर्ता की पुष्टि करनी है जो ब्राउज़र नहीं है, तो आपको साइन-इन फ़्लो को मैन्युअल तरीके से मैनेज करना होगा.)
Firebase JavaScript SDK टूल की मदद से साइन-इन फ़्लो को मैनेज करने के लिए, यह तरीका अपनाएं:
- Twitter की सेवा देने वाली कंपनी के ऑब्जेक्ट का इंस्टेंस बनाएं:
import { TwitterAuthProvider } from "firebase/auth"; const provider = new TwitterAuthProvider();
var provider = new firebase.auth.TwitterAuthProvider();
- ज़रूरी नहीं: OAuth फ़्लो शुरू करने से पहले, Auth इंस्टेंस पर भाषा कोड अपडेट करें. इससे, उपयोगकर्ता की पसंदीदा भाषा में, सेवा देने वाली कंपनी के OAuth फ़्लो को स्थानीय भाषा में बदला जा सकता है. इसके लिए, OAuth फ़्लो शुरू करने से पहले, ज़रूरी कस्टम OAuth पैरामीटर को साफ़ तौर पर पास करने की ज़रूरत नहीं होती. उदाहरण के लिए:
import { getAuth } from "firebase/auth"; const auth = getAuth(); auth.languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // auth.useDeviceLanguage();
firebase.auth().languageCode = 'it'; // To apply the default browser preference instead of explicitly setting it. // firebase.auth().useDeviceLanguage();
- ज़रूरी नहीं: OAuth प्रोवाइडर के ऐसे अतिरिक्त कस्टम पैरामीटर बताएं
जिन्हें आपको OAuth अनुरोध के साथ भेजना है. कस्टम पैरामीटर जोड़ने के लिए, शुरू किए गए प्रोवाइडर पर
setCustomParameters
को कॉल करें. इसके लिए, OAuth प्रोवाइडर के दस्तावेज़ में बताई गई कुंजी और उससे जुड़ी वैल्यू वाला ऑब्जेक्ट इस्तेमाल करें. उदाहरण के लिए:provider.setCustomParameters({ 'lang': 'es' });
provider.setCustomParameters({ 'lang': 'es' });
- Twitter प्रोवाइडर ऑब्जेक्ट का इस्तेमाल करके, Firebase से पुष्टि करें. आपके पास अपने उपयोगकर्ताओं को, अपने Twitter खातों से साइन इन करने के लिए कहने का विकल्प है. इसके लिए, पॉप-अप विंडो खोलें या साइन-इन पेज पर रीडायरेक्ट करें. मोबाइल डिवाइसों पर, रीडायरेक्ट करने का तरीका इस्तेमाल करना बेहतर होता है.
- पॉप-अप विंडो से साइन इन करने के लिए,
signInWithPopup
को कॉल करें:import { getAuth, signInWithPopup, TwitterAuthProvider } from "firebase/auth"; const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. const credential = TwitterAuthProvider.credentialFromResult(result); const token = credential.accessToken; const secret = credential.secret; // The signed-in user info. const user = result.user; // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = TwitterAuthProvider.credentialFromError(error); // ... });
firebase .auth() .signInWithPopup(provider) .then((result) => { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. var token = credential.accessToken; var secret = credential.secret; // The signed-in user info. var user = result.user; // IdP data available in result.additionalUserInfo.profile. // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
यहां गड़बड़ियों का पता लगाया जा सकता है और उन्हें ठीक किया जा सकता है. गड़बड़ी के कोड की सूची के लिए, पुष्टि करने से जुड़े रेफ़रंस दस्तावेज़ देखें.
- साइन इन पेज पर रीडायरेक्ट करके साइन इन करने के लिए,
signInWithRedirect
को कॉल करें: `signInWithRedirect` का इस्तेमाल करते समय, सबसे सही तरीके अपनाएं.import { getAuth, signInWithRedirect } from "firebase/auth"; const auth = getAuth(); signInWithRedirect(auth, provider);
firebase.auth().signInWithRedirect(provider);
getRedirectResult
को कॉल करके, Twitter की सेवा देने वाली कंपनी का OAuth टोकन भी वापस पाया जा सकता है:import { getAuth, getRedirectResult, TwitterAuthProvider } from "firebase/auth"; const auth = getAuth(); getRedirectResult(auth) .then((result) => { // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. const credential = TwitterAuthProvider.credentialFromResult(result); const token = credential.accessToken; const secret = credential.secret; // ... // The signed-in user info. const user = result.user; // IdP data available using getAdditionalUserInfo(result) // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = TwitterAuthProvider.credentialFromError(error); // ... });
firebase.auth() .getRedirectResult() .then((result) => { if (result.credential) { /** @type {firebase.auth.OAuthCredential} */ var credential = result.credential; // This gives you a the Twitter OAuth 1.0 Access Token and Secret. // You can use these server side with your app's credentials to access the Twitter API. var token = credential.accessToken; var secret = credential.secret; // ... } // The signed-in user info. var user = result.user; // IdP data available in result.additionalUserInfo.profile. // ... }).catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
- पॉप-अप विंडो से साइन इन करने के लिए,
'खाता पहले से मौजूद है और उसके लिए अलग क्रेडेंशियल इस्तेमाल किए गए हैं' वाली गड़बड़ियों को मैनेज करना
अगर आपने Firebase कंसोल में हर ईमेल पते के लिए एक खाता सेटिंग चालू की है, तो जब कोई उपयोगकर्ता किसी ईमेल पते से किसी सेवा देने वाली कंपनी (जैसे, Twitter) में साइन इन करने की कोशिश करता है, जो पहले से ही Firebase के किसी दूसरे उपयोगकर्ता की सेवा देने वाली कंपनी (जैसे, Google) के लिए मौजूद है, तो उसे auth/account-exists-with-different-credential
गड़बड़ी का मैसेज मिलता है. साथ ही, AuthCredential
ऑब्जेक्ट (Twitter oauth टोकन और सीक्रेट) भी दिखता है. उपयोगकर्ता को जिस सेवा देने वाली कंपनी में साइन इन करना है उसमें साइन इन करने के लिए, उसे पहले मौजूदा सेवा देने वाली कंपनी (Google) में साइन इन करना होगा. इसके बाद, उसे पहले इस्तेमाल किए गए AuthCredential
(Twitter oauth टोकन और सीक्रेट) से लिंक करना होगा.
पॉप-अप मोड
signInWithPopup
का इस्तेमाल करने पर, auth/account-exists-with-different-credential
गड़बड़ियों को इस तरह के कोड की मदद से मैनेज किया जा सकता है:
import { getAuth, linkWithCredential, signInWithPopup, TwitterAuthProvider, } from "firebase/auth"; try { // Step 1: User tries to sign in using Twitter. let result = await signInWithPopup(getAuth(), new TwitterAuthProvider()); } catch (error) { // Step 2: User's email already exists. if (error.code === "auth/account-exists-with-different-credential") { // The pending Twitter credential. let pendingCred = error.credential; // Step 3: Save the pending credential in temporary storage, // Step 4: Let the user know that they already have an account // but with a different provider, and let them choose another // sign-in method. } } // ... try { // Step 5: Sign the user in using their chosen method. let result = await signInWithPopup(getAuth(), userSelectedProvider); // Step 6: Link to the Twitter credential. // TODO: implement `retrievePendingCred` for your app. let pendingCred = retrievePendingCred(); if (pendingCred !== null) { // As you have access to the pending credential, you can directly call the // link method. let user = await linkWithCredential(result.user, pendingCred); } // Step 7: Continue to app. } catch (error) { // ... }
रीडायरेक्ट मोड
रीडायरेक्ट मोड में भी इस गड़बड़ी को इसी तरह मैनेज किया जाता है. हालांकि, इस मोड में पेज रीडायरेक्ट के बीच, पुष्टि बाकी होने वाले क्रेडेंशियल को कैश मेमोरी में सेव करना पड़ता है. उदाहरण के लिए, सेशन स्टोरेज का इस्तेमाल करके.
साइन इन फ़्लो को मैन्युअल तरीके से मैनेज करना
Twitter खाते का इस्तेमाल करके भी Firebase से पुष्टि की जा सकती है. इसके लिए, आपको Twitter OAuth एंडपॉइंट को कॉल करके, साइन-इन फ़्लो को मैनेज करना होगा:
- डेवलपर के दस्तावेज़ में दिए गए निर्देशों का पालन करके, अपने ऐप्लिकेशन में Twitter की पुष्टि करने की सुविधा को इंटिग्रेट करें. Twitter में साइन-इन करने के फ़्लो के आखिर में, आपको एक OAuth ऐक्सेस टोकन और एक OAuth सीक्रेट मिलेगा.
- अगर आपको किसी Node.js ऐप्लिकेशन में साइन इन करना है, तो Node.js ऐप्लिकेशन को OAuth ऐक्सेस टोकन और OAuth सीक्रेट भेजें.
- जब कोई उपयोगकर्ता Twitter से साइन इन कर लेता है, तो OAuth ऐक्सेस टोकन और OAuth सीक्रेट को Firebase क्रेडेंशियल से बदलें:
var credential = firebase.auth.TwitterAuthProvider.credential(token, secret);
- Firebase क्रेडेंशियल का इस्तेमाल करके, Firebase से पुष्टि करना:
import { getAuth, signInWithCredential, FacebookAuthProvider } from "firebase/auth"; // Sign in with the credential from the Facebook user. const auth = getAuth(); signInWithCredential(auth, credential) .then((result) => { // Signed in const credential = FacebookAuthProvider.credentialFromResult(result); }) .catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.customData.email; // The AuthCredential type that was used. const credential = FacebookAuthProvider.credentialFromError(error); // ... });
// Sign in with the credential from the Facebook user. firebase.auth().signInWithCredential(credential) .then((result) => { // Signed in var credential = result.credential; // ... }) .catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... });
Chrome एक्सटेंशन में Firebase की मदद से पुष्टि करना
अगर आपको Chrome एक्सटेंशन ऐप्लिकेशन बनाना है, तो ऑफ़स्क्रीन दस्तावेज़ों से जुड़ी गाइड देखें.
Twitter साइन इन के लिए, रीडायरेक्ट डोमेन को पसंद के मुताबिक बनाना
प्रोजेक्ट बनाने पर, Firebase आपके प्रोजेक्ट के लिए एक यूनीक सबडोमेन उपलब्ध कराएगा:
https://my-app-12345.firebaseapp.com
.
इसका इस्तेमाल, OAuth साइन इन के लिए रीडायरेक्ट करने के तरीके के तौर पर भी किया जाएगा. उस डोमेन को, OAuth की सुविधा देने वाली सभी कंपनियों के लिए अनुमति दी जानी चाहिए. हालांकि, इसका मतलब है कि उपयोगकर्ताओं को ऐप्लिकेशन पर वापस रीडायरेक्ट करने से पहले, Twitter में साइन इन करते समय वह डोमेन दिख सकता है: इस पर जाएं: https://my-app-12345.firebaseapp.com.
अपना सबडोमेन न दिखाने के लिए, Firebase Hosting की मदद से कस्टम डोमेन सेट अप किया जा सकता है:
- Hosting के लिए अपना डोमेन सेट अप करें में दिए गए पहले से तीसरे चरण को पूरा करें. डोमेन के मालिकाना हक की पुष्टि करने पर, Hosting आपके कस्टम डोमेन के लिए एसएसएल सर्टिफ़िकेट उपलब्ध कराता है.
- Firebase कंसोल में, अनुमति वाले डोमेन की सूची में अपना कस्टम डोमेन जोड़ें:
auth.custom.domain.com
. - Twitter डेवलपर कंसोल या OAuth सेटअप पेज में, रीडायरेक्ट पेज के यूआरएल को व्हाइटलिस्ट करें. यह यूआरएल, आपके कस्टम डोमेन:
https://auth.custom.domain.com/__/auth/handler
पर ऐक्सेस किया जा सकेगा. - JavaScript लाइब्रेरी को शुरू करते समय,
authDomain
फ़ील्ड की मदद से अपना कस्टम डोमेन डालें:var config = { apiKey: '...', // Changed from '
PROJECT_ID .firebaseapp.com'. authDomain: 'auth.custom.domain.com', databaseURL: 'https://PROJECT_ID .firebaseio.com', projectId: 'PROJECT_ID ', storageBucket: ' ', messagingSenderId: 'PROJECT_ID .firebasestorage.appSENDER_ID ' }; firebase.initializeApp(config);
अगले चरण
जब कोई उपयोगकर्ता पहली बार साइन इन करता है, तो उसके लिए नया उपयोगकर्ता खाता बन जाता है. साथ ही, उस खाते को उन क्रेडेंशियल से लिंक कर दिया जाता है जिनका इस्तेमाल करके उसने साइन इन किया था. जैसे, उपयोगकर्ता का नाम और पासवर्ड, फ़ोन नंबर या पुष्टि करने वाली सेवा की जानकारी. यह नया खाता, आपके Firebase प्रोजेक्ट के हिस्से के तौर पर सेव किया जाता है. इसका इस्तेमाल, आपके प्रोजेक्ट के हर ऐप्लिकेशन में किसी उपयोगकर्ता की पहचान करने के लिए किया जा सकता है. भले ही, उपयोगकर्ता साइन इन करने का तरीका कोई भी हो.
-
अपने ऐप्लिकेशन में, उपयोगकर्ता की पुष्टि की स्थिति जानने के लिए,
Auth
ऑब्जेक्ट पर ऑब्ज़र्वर सेट करने का सुझाव दिया जाता है. इसके बाद,User
ऑब्जेक्ट से उपयोगकर्ता की प्रोफ़ाइल की बुनियादी जानकारी पाई जा सकती है. उपयोगकर्ताओं को मैनेज करें देखें. अपने Firebase Realtime Database और Cloud Storage सुरक्षा नियमों में, आपके पास
auth
वैरिएबल से साइन इन किए गए उपयोगकर्ता का यूनीक उपयोगकर्ता आईडी पाने का विकल्प है. साथ ही, इसका इस्तेमाल करके यह कंट्रोल किया जा सकता है कि उपयोगकर्ता कौनसा डेटा ऐक्सेस कर सकता है.
पुष्टि करने वाली सेवा देने वाली कंपनी के क्रेडेंशियल को किसी मौजूदा उपयोगकर्ता खाते से लिंक करके, उपयोगकर्ताओं को पुष्टि करने वाली कई कंपनियों का इस्तेमाल करके, आपके ऐप्लिकेशन में साइन इन करने की अनुमति दी जा सकती है.
किसी उपयोगकर्ता को साइन आउट करने के लिए,
signOut
को कॉल करें:
import { getAuth, signOut } from "firebase/auth"; const auth = getAuth(); signOut(auth).then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });
firebase.auth().signOut().then(() => { // Sign-out successful. }).catch((error) => { // An error happened. });