आप अपने उपयोगकर्ताओं को उनके GitHub खातों का इस्तेमाल करके, Firebase से पुष्टि करने की अनुमति दे सकते हैं इसके लिए, अपने ऐप्लिकेशन में GitHub की पुष्टि करें. GitHub को इंटिग्रेट किया जा सकता है पुष्टि करने के लिए, Firebase SDK टूल का इस्तेमाल करके साइन-इन फ़्लो को पूरा किया जा सकता है, या GitHub OAuth 2.0 फ़्लो को मैन्युअल तरीके से चलाकर, Firebase के लिए ऐक्सेस टोकन.
शुरू करने से पहले
- Firebase को अपने JavaScript प्रोजेक्ट में जोड़ें.
- Firebase कंसोल में, पुष्टि सेक्शन खोलें.
- साइन इन करने का तरीका टैब पर, GitHub सेवा देने वाली कंपनी को चालू करें.
- उस कंपनी के डेवलपर कंसोल से क्लाइंट आईडी और क्लाइंट सीक्रेट जोड़ें
प्रोवाइडर कॉन्फ़िगरेशन:
- अपना ऐप्लिकेशन रजिस्टर करना GitHub पर डेवलपर ऐप्लिकेशन के तौर पर जोड़ें और अपने ऐप्लिकेशन का OAuth 2.0 क्लाइंट आईडी पाएं और क्लाइंट सीक्रेट शामिल हैं.
- पक्का करें कि आपका Firebase OAuth रीडायरेक्ट यूआरआई (उदाहरण,
my-app-12345.firebaseapp.com/__/auth/handler
) को आपके ऐप्लिकेशन के सेटिंग पेज में ऑथराइज़ेशन कॉलबैक यूआरएल के तौर पर सेट किया गया है GitHub ऐप्लिकेशन का कॉन्फ़िगरेशन.
- सेव करें पर क्लिक करें.
Firebase SDK टूल की मदद से साइन-इन की प्रोसेस को मैनेज करना
अगर आप कोई वेब ऐप्लिकेशन बना रहे हैं, तो अपने उपयोगकर्ताओं की पुष्टि करने का यह सबसे आसान तरीका है यह तरीका, GitHub खातों का इस्तेमाल करके Firebase को Firebase JavaScript SDK टूल. (अगर आप Node.js में किसी उपयोगकर्ता की पुष्टि करना चाहते हैं या अन्य गैर-ब्राउज़र एनवायरमेंट का इस्तेमाल करने के लिए, आपको साइन-इन फ़्लो को मैन्युअल तौर पर मैनेज करना होगा.
Firebase JavaScript SDK टूल से साइन-इन फ़्लो को मैनेज करने के लिए, इन निर्देशों का पालन करें चरण:
- GitHub प्रोवाइडर ऑब्जेक्ट का इंस्टेंस बनाएं:
import { GithubAuthProvider } from "firebase/auth";
const provider = new GithubAuthProvider();var provider = new firebase.auth.GithubAuthProvider();
- ज़रूरी नहीं: OAuth 2.0 के वे अन्य स्कोप तय किए जा सकते हैं जिनके लिए आपने अपने Google खाते में साइन इन किया है
पुष्टि करने वाली कंपनी से अनुरोध करना है. दायरा जोड़ने के लिए, कॉल करें
addScope
. उदाहरण के लिए:provider.addScope('repo');
provider.addScope('repo');
- ज़रूरी नहीं: OAuth की सेवा देने वाली कंपनी के अन्य पैरामीटर तय करना
जिसे आपको OAuth अनुरोध की मदद से भेजना है. कस्टम पैरामीटर जोड़ने के लिए,
कुंजी वाले ऑब्जेक्ट के साथ शुरू की गई कंपनी के लिए
setCustomParameters
जैसा कि OAuth प्रोवाइडर के दस्तावेज़ और संबंधित वैल्यू में बताया गया है. उदाहरण के लिए:provider.setCustomParameters({
'allow_signup': 'false'
});provider.setCustomParameters({
'allow_signup': 'false'
}); - GitHub प्रोवाइडर ऑब्जेक्ट का इस्तेमाल करके, Firebase से पुष्टि करें. आप
अपने उपयोगकर्ताओं को उनके GitHub खातों से साइन इन करने के लिए कहें
पॉप-अप विंडो पर क्लिक करें या साइन-इन पेज पर रीडायरेक्ट करें. रीडायरेक्ट करने का तरीका
मोबाइल डिवाइस पर प्राथमिकता दी जाती है.
- पॉप-अप विंडो की मदद से साइन इन करने के लिए,
signInWithPopup
पर कॉल करें:import { getAuth, signInWithPopup, GithubAuthProvider } from "firebase/auth";
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
const credential = GithubAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// 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 = GithubAuthProvider.credentialFromError(error);
// ...
});firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
var token = credential.accessToken;
// 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, GithubAuthProvider } from "firebase/auth";
const auth = getAuth();
getRedirectResult(auth)
.then((result) => {
const credential = GithubAuthProvider.credentialFromResult(result);
if (credential) {
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
const token = credential.accessToken;
// ...
}
// 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 = GithubAuthProvider.credentialFromError(error);
// ...
});firebase.auth()
.getRedirectResult()
.then((result) => {
if (result.credential) {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// This gives you a GitHub Access Token. You can use it to access the GitHub API.
var token = credential.accessToken;
// ...
}
// 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 कंसोल में, हर ईमेल पते के लिए एक खाता सेटिंग चालू की है, तो
जब कोई उपयोगकर्ता पहले से ही किसी ईमेल से, सेवा देने वाली किसी कंपनी (जैसे कि GitHub) में साइन इन करने की कोशिश करता है
Firebase का इस्तेमाल करने वाले किसी दूसरे व्यक्ति को सेवा देने वाली कंपनी (जैसे, Google) के लिए मौजूद है, तो गड़बड़ी
auth/account-exists-with-different-credential
को एक
AuthCredential
ऑब्जेक्ट (GitHub ऐक्सेस टोकन). साइन इन करने के लिए,
सेवा देने वाली मौजूदा कंपनी के लिए, उपयोगकर्ता को पहले मौजूदा कंपनी (Google) से साइन करना होगा और फिर
पुराना AuthCredential
(GitHub ऐक्सेस टोकन).
पॉप-अप मोड
अगर आप signInWithPopup
का इस्तेमाल करते हैं, तो
इस तरह के कोड वाली auth/account-exists-with-different-credential
गड़बड़ियां
उदाहरण:
import {
getAuth,
linkWithCredential,
signInWithPopup,
GitHubAuthProvider,
} from "firebase/auth";
try {
// Step 1: User tries to sign in using GitHub.
let result = await signInWithPopup(getAuth(), new GitHubAuthProvider());
} catch (error) {
// Step 2: User's email already exists.
if (error.code === "auth/account-exists-with-different-credential") {
// The pending GitHub 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 GitHub 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) {
// ...
}
रीडायरेक्ट मोड
रीडायरेक्ट मोड में इस गड़बड़ी को ठीक इसी तरह से हैंडल किया जाता है. इसमें अंतर यह होता है कि क्रेडेंशियल को पेज रीडायरेक्ट के बीच कैश मेमोरी में रखना चाहिए. उदाहरण के लिए, सेशन स्टोरेज का इस्तेमाल करना.
साइन-इन की प्रोसेस को मैन्युअल तरीके से मैनेज करना
आप GitHub OAuth 2.0 एंडपॉइंट को कॉल करके साइन-इन फ़्लो:
- यहां दिए गए निर्देशों का पालन करके, अपने ऐप्लिकेशन में GitHub की पुष्टि करने की सुविधा इंटिग्रेट करें डेवलपर के दस्तावेज़ में दिए गए हैं. GitHub की साइन इन प्रोसेस के आखिर में, को OAuth 2.0 ऐक्सेस टोकन मिलेगा.
- अगर आपको Node.js ऐप्लिकेशन पर साइन इन करना है, तो OAuth का ऐक्सेस भेजें टोकन को Node.js ऐप्लिकेशन पर भेजना होगा.
- जब कोई उपयोगकर्ता GitHub से साइन इन कर ले, तब OAuth 2.0 को एक्सचेंज करें
Firebase क्रेडेंशियल के लिए ऐक्सेस टोकन:
import { GithubAuthProvider } from "firebase/auth";
const credential = GithubAuthProvider.credential(token);var credential = firebase.auth.GithubAuthProvider.credential(token);
- Firebase क्रेडेंशियल का इस्तेमाल करके, Firebase से पुष्टि करें:
import { getAuth, signInWithCredential } from "firebase/auth";
// Sign in with the credential from the user.
const auth = getAuth();
signInWithCredential(auth, credential)
.then((result) => {
// Signed in
// ...
})
.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;
// ...
});// Sign in with the credential from the user.
firebase.auth()
.signInWithCredential(credential)
.then((result) => {
// Signed in
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// ...
});
Chrome एक्सटेंशन में Firebase की मदद से पुष्टि करें
यदि आप कोई Chrome एक्सटेंशन ऐप्लिकेशन बना रहे हैं, तो ऑफ़स्क्रीन दस्तावेज़ गाइड.
GitHub में साइन इन करने के लिए रीडायरेक्ट डोमेन को पसंद के मुताबिक बनाना
प्रोजेक्ट बनाते समय, Firebase आपके प्रोजेक्ट के लिए एक यूनीक सबडोमेन का प्रावधान करेगा:
https://my-app-12345.firebaseapp.com
.
इसका इस्तेमाल, OAuth में साइन इन करने के लिए रीडायरेक्ट करने के तरीके के तौर पर भी किया जाएगा. उस डोमेन को की अनुमति है. हालांकि, इसका मतलब है कि लोगों को ऐप्लिकेशन पर वापस रीडायरेक्ट करने से पहले GitHub में साइन इन करते समय डोमेन नेम को सेट अप करने के लिए: इस पर जारी रखें: https://my-app-12345.firebaseapp.com.
अगर आपको अपना सबडोमेन नहीं दिखाना है, तो Firebase Hosting का इस्तेमाल करके कस्टम डोमेन सेट अप करें:
- पहले से लेकर तीसरे चरण का पालन करें Hosting के लिए अपना डोमेन सेट अप करें. पुष्टि करने पर आपके डोमेन का स्वामित्व, Hosting आपके कस्टम डोमेन के लिए एक SSL प्रमाणपत्र का प्रावधान करता है.
- अपने कस्टम डोमेन को
Firebase कंसोल:
auth.custom.domain.com
. - GitHub डेवलपर कंसोल या 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.
});