उपयोगकर्ताओं को उनके Twitter खातों का इस्तेमाल करके, Firebase की मदद से पुष्टि करने की अनुमति दी जा सकती है अपने ऐप्लिकेशन में Twitter ऑथेंटिकेशन को इंटिग्रेट करके. आप Twitter को एक साथ जोड़ सकते हैं पुष्टि करने के लिए, Firebase SDK टूल का इस्तेमाल करके साइन-इन फ़्लो को पूरा किया जा सकता है, या Twitter OAuth फ़्लो को मैन्युअल तरीके से चलाकर, ऐक्सेस टोकन और Firebase के लिए सीक्रेट.
शुरू करने से पहले
- Firebase को अपने JavaScript प्रोजेक्ट में जोड़ें.
- Firebase कंसोल में, पुष्टि सेक्शन खोलें.
- साइन इन करने का तरीका टैब पर, Twitter की सेवा देने वाली कंपनी को चालू करें.
- उस कंपनी के डेवलपर कंसोल से एपीआई पासकोड और एपीआई सीक्रेट को
प्रोवाइडर कॉन्फ़िगरेशन:
- अपना ऐप्लिकेशन रजिस्टर करना को भी इंस्टॉल करें और अपने ऐप्लिकेशन की OAuth एपीआई पासकोड पाएं और एपीआई सीक्रेट शामिल है.
- पक्का करें कि आपका Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण,
my-app-12345.firebaseapp.com/__/auth/handler
) को आपके ऐप्लिकेशन के सेटिंग पेज में ऑथराइज़ेशन कॉलबैक यूआरएल के तौर पर सेट किया गया है Twitter ऐप्लिकेशन का कॉन्फ़िगरेशन.
- सेव करें पर क्लिक करें.
Firebase SDK टूल की मदद से साइन-इन की प्रोसेस को मैनेज करना
अगर आप कोई वेब ऐप्लिकेशन बना रहे हैं, तो अपने उपयोगकर्ताओं की पुष्टि करने का यह सबसे आसान तरीका है के साथ साइन-इन फ़्लो को मैनेज करना है. Firebase JavaScript SDK टूल. (अगर आप Node.js में किसी उपयोगकर्ता की पुष्टि करना चाहते हैं या अन्य गैर-ब्राउज़र एनवायरमेंट का इस्तेमाल करने के लिए, आपको साइन-इन फ़्लो को मैन्युअल तौर पर मैनेज करना होगा.
Firebase JavaScript SDK टूल से साइन-इन फ़्लो को मैनेज करने के लिए, इन निर्देशों का पालन करें चरण:
- Twitter कंपनी के ऑब्जेक्ट का इंस्टेंस बनाएं:
import { TwitterAuthProvider } from "firebase/auth";
const provider = new TwitterAuthProvider();var provider = new firebase.auth.TwitterAuthProvider();
- ज़रूरी नहीं: सेवा देने वाली कंपनी के OAuth फ़्लो को उपयोगकर्ता की पसंद के मुताबिक बनाने के लिए
मौजूदा कस्टम OAuth पैरामीटर को साफ़ तौर पर पास किए बिना, इस भाषा में बदलाव करें
OAuth फ़्लो शुरू करने से पहले, Auth इंस्टेंस पर कोड. उदाहरण के लिए:
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
: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 OAuth एंडपॉइंट पर कॉल करके साइन-इन फ़्लो:
- यहां दिए गए निर्देशों का पालन करके, अपने ऐप्लिकेशन में Twitter की पुष्टि करने की प्रक्रिया को इंटिग्रेट करें डेवलपर के दस्तावेज़ में दिए गए हैं. Twitter पर साइन-इन करने की प्रोसेस के आखिर में, को एक OAuth ऐक्सेस टोकन और एक OAuth सीक्रेट मिलेगा.
- अगर आपको Node.js ऐप्लिकेशन पर साइन इन करना है, तो OAuth का ऐक्सेस भेजें टोकन और OAuth सीक्रेट को Node.js ऐप्लिकेशन पर ट्रांसफ़र करेगा.
- जब कोई उपयोगकर्ता Twitter से साइन इन कर लेता है, तब OAuth के ऐक्सेस को एक्सचेंज करें
Firebase क्रेडेंशियल के लिए टोकन और OAuth सीक्रेट:
अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है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 में साइन इन करने के लिए रीडायरेक्ट करने के तरीके के तौर पर भी किया जाएगा. उस डोमेन को की अनुमति है. हालांकि, इसका मतलब है कि लोगों को ऐप्लिकेशन पर वापस रीडायरेक्ट करने से पहले Twitter में साइन इन करते समय अपने डोमेन को अपडेट करने के लिए: इस पर जारी रखें: https://my-app-12345.firebaseapp.com.
अगर आपको अपना सबडोमेन नहीं दिखाना है, तो Firebase Hosting का इस्तेमाल करके कस्टम डोमेन सेट अप करें:
- पहले से लेकर तीसरे चरण का पालन करें Hosting के लिए अपना डोमेन सेट अप करें. पुष्टि करने पर आपके डोमेन का स्वामित्व, Hosting आपके कस्टम डोमेन के लिए एक SSL प्रमाणपत्र का प्रावधान करता है.
- अपने कस्टम डोमेन को
Firebase कंसोल:
auth.custom.domain.com
. - Twitter डेवलपर कंसोल या OAuth सेटअप पेज में, रीडायरेक्ट पेज के यूआरएल को वाइटलिस्ट करें,
जो आपके कस्टम डोमेन पर ऐक्सेस किए जा सकेंगे:
https://auth.custom.domain.com/__/auth/handler
. - JavaScript लाइब्रेरी का इस्तेमाल शुरू करते समय, अपने कस्टम डोमेन को
authDomain
फ़ील्ड:var config = {
apiKey: '...',
// Changed from 'my-app-12345.firebaseapp.com'.
authDomain: 'auth.custom.domain.com',
databaseURL: 'https://my-app-12345.firebaseio.com',
projectId: 'my-app-12345',
storageBucket: 'my-app-12345.appspot.com',
messagingSenderId: '1234567890'
};
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.
});