यह दस्तावेज़ आपको दिखाता है कि मेनिफेस्ट V3 का उपयोग करने वाले Chrome एक्सटेंशन में उपयोगकर्ताओं को साइन करने के लिए फ़ायरबेस प्रमाणीकरण का उपयोग कैसे करें।
फायरबेस प्रमाणीकरण क्रोम एक्सटेंशन से उपयोगकर्ताओं को साइन इन करने के लिए कई प्रमाणीकरण विधियां प्रदान करता है, जिनमें से कुछ को दूसरों की तुलना में अधिक विकास प्रयास की आवश्यकता होती है।
मेनिफेस्ट V3 क्रोम एक्सटेंशन में निम्नलिखित विधियों का उपयोग करने के लिए, आपको केवल उन्हें firebase/auth/web-extension
से आयात करने की आवश्यकता है:
- ईमेल और पासवर्ड से साइन इन करें (
createUserWithEmailAndPassword
औरsignInWithEmailAndPassword
) - ईमेल लिंक से साइन इन करें (
sendSignInLinkToEmail
,isSignInWithEmailLink
, औरsignInWithEmailLink
) - गुमनाम रूप से साइन इन करें (
signInAnonymously
) - कस्टम प्रमाणीकरण प्रणाली के साथ साइन इन करें (
signInWithCustomToken
) - प्रदाता साइन-इन को स्वतंत्र रूप से संभालें, फिर
signInWithCredential
उपयोग करें
निम्नलिखित साइन इन विधियाँ भी समर्थित हैं लेकिन कुछ अतिरिक्त कार्य की आवश्यकता है:
- एक पॉप-अप विंडो के साथ साइन इन करें (
signInWithPopup
,linkWithPopup
, औरreauthenticateWithPopup
) - साइन-इन पेज पर रीडायरेक्ट करके साइन इन करें (
signInWithRedirect
,linkWithRedirect
, औरreauthenticateWithRedirect
) - रीकैप्चा वाले फ़ोन नंबर से साइन इन करें
- रीकैप्चा के साथ एसएमएस बहु-कारक प्रमाणीकरण
- reCAPTCHA एंटरप्राइज़ सुरक्षा
मेनिफेस्ट V3 क्रोम एक्सटेंशन में इन विधियों का उपयोग करने के लिए, आपको ऑफस्क्रीन दस्तावेज़ का उपयोग करना होगा।
फायरबेस/ऑथ/वेब-एक्सटेंशन प्रविष्टि बिंदु का उपयोग करें
firebase/auth/web-extension
से आयात करने से उपयोगकर्ता वेब ऐप के समान क्रोम एक्सटेंशन से साइन इन कर सकते हैं।
फायरबेस/ऑथ/वेब-एक्सटेंशन केवल वेब एसडीके संस्करण v10.8.0 और उससे ऊपर पर समर्थित है।
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth/web-extension'; const auth = getAuth(); signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { // Signed in const user = userCredential.user; // ... }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; });
ऑफस्क्रीन दस्तावेज़ों का उपयोग करें
कुछ प्रमाणीकरण विधियाँ, जैसे कि signInWithPopup
, linkWithPopup
, और reauthenticateWithPopup
, क्रोम एक्सटेंशन के साथ सीधे संगत नहीं हैं, क्योंकि उन्हें एक्सटेंशन पैकेज के बाहर से कोड लोड करने की आवश्यकता होती है। मेनिफेस्ट V3 में प्रारंभ होने पर, इसकी अनुमति नहीं है और इसे एक्सटेंशन प्लेटफ़ॉर्म द्वारा अवरुद्ध कर दिया जाएगा। इससे निजात पाने के लिए, आप एक ऑफस्क्रीन दस्तावेज़ का उपयोग करके उस कोड को एक आईफ्रेम के भीतर लोड कर सकते हैं। ऑफस्क्रीन दस्तावेज़ में, सामान्य प्रमाणीकरण प्रवाह लागू करें और परिणाम को ऑफस्क्रीन दस्तावेज़ से वापस एक्सटेंशन पर प्रॉक्सी करें।
यह मार्गदर्शिका एक उदाहरण के रूप में signInWithPopup
का उपयोग करती है, लेकिन यही अवधारणा अन्य प्रमाणीकरण विधियों पर भी लागू होती है।
शुरू करने से पहले
इस तकनीक के लिए आपको एक वेब पेज सेट करना होगा जो वेब पर उपलब्ध है, जिसे आप एक आईफ्रेम में लोड करेंगे। कोई भी होस्ट इसके लिए काम करता है, जिसमें फायरबेस होस्टिंग भी शामिल है। निम्नलिखित सामग्री के साथ एक वेबसाइट बनाएं:
<!DOCTYPE html> <html> <head> <title>signInWithPopup</title> <script src="signInWithPopup.js"></script> </head> <body><h1>signInWithPopup</h1></body> </html>
फ़ेडरेटेड साइन इन करें
यदि आप फ़ेडरेटेड साइन इन का उपयोग कर रहे हैं, जैसे कि Google, Apple, SAML, या OIDC के साथ साइन इन, तो आपको अपनी Chrome एक्सटेंशन आईडी को अधिकृत डोमेन की सूची में जोड़ना होगा:
- अपना प्रोजेक्ट फायरबेस कंसोल में खोलें।
- प्रमाणीकरण अनुभाग में, सेटिंग पृष्ठ खोलें।
- अधिकृत डोमेन की सूची में निम्नलिखित की तरह एक यूआरआई जोड़ें:
chrome-extension://CHROME_EXTENSION_ID
अपने Chrome एक्सटेंशन की मेनिफेस्ट फ़ाइल में सुनिश्चित करें कि आप निम्नलिखित URL को content_security_policy
अनुमति सूची में जोड़ें:
-
https://apis.google.com
-
https://www.gstatic.com
-
https://www.googleapis.com
-
https://securetoken.googleapis.com
प्रमाणीकरण लागू करें
आपके HTML दस्तावेज़ में,signInWithPopup.js जावास्क्रिप्ट कोड है जो प्रमाणीकरण को संभालता है। किसी विधि को लागू करने के दो अलग-अलग तरीके हैं जो सीधे एक्सटेंशन में समर्थित हैं:
- firebase/
firebase/auth/web-extension
के बजायfirebase/auth
उपयोग करें।web-extension
प्रविष्टि बिंदु एक्सटेंशन के भीतर चल रहे कोड के लिए है। हालाँकि यह कोड अंततः एक्सटेंशन में चलता है (iframe में, आपके ऑफस्क्रीन दस्तावेज़ में), यह जिस संदर्भ में चल रहा है वह मानक वेब है। - प्रमाणीकरण अनुरोध और प्रतिक्रिया को प्रॉक्सी करने के लिए, प्रमाणीकरण तर्क को
postMessage
श्रोता में लपेटें।
import { signInWithPopup, GoogleAuthProvider, getAuth } from'firebase/auth'; import { initializeApp } from 'firebase/app'; import firebaseConfig from './firebaseConfig.js' const app = initializeApp(firebaseConfig); const auth = getAuth(); // This code runs inside of an iframe in the extension's offscreen document. // This gives you a reference to the parent frame, i.e. the offscreen document. // You will need this to assign the targetOrigin for postMessage. const PARENT_FRAME = document.location.ancestorOrigins[0]; // This demo uses the Google auth provider, but any supported provider works. // Make sure that you enable any provider you want to use in the Firebase Console. // https://console.firebase.google.com/project/_/authentication/providers const PROVIDER = new GoogleAuthProvider(); function sendResponse(result) { globalThis.parent.self.postMessage(JSON.stringify(result), PARENT_FRAME); } globalThis.addEventListener('message', function({data}) { if (data.initAuth) { // Opens the Google sign-in page in a popup, inside of an iframe in the // extension's offscreen document. // To centralize logic, all respones are forwarded to the parent frame, // which goes on to forward them to the extension's service worker. signInWithPopup(auth, PROVIDER) .then(sendResponse) .catch(sendResponse) } });
अपना Chrome एक्सटेंशन बनाएं
आपकी वेबसाइट लाइव होने के बाद आप इसे अपने क्रोम एक्सटेंशन में उपयोग कर सकते हैं।
- अपनी मैनिफ़ेस्ट.json फ़ाइल में
offscreen
अनुमति जोड़ें: - ऑफस्क्रीन दस्तावेज़ स्वयं बनाएं. यह आपके एक्सटेंशन पैकेज के अंदर एक न्यूनतम HTML फ़ाइल है जो आपके ऑफस्क्रीन दस्तावेज़ जावास्क्रिप्ट के तर्क को लोड करती है:
- अपने एक्सटेंशन पैकेज में
offscreen.js
शामिल करें। यह चरण 1 में स्थापित सार्वजनिक वेबसाइट और आपके एक्सटेंशन के बीच प्रॉक्सी के रूप में कार्य करता है। - अपने बैकग्राउंड.जेएस सेवा कर्मी से ऑफस्क्रीन दस्तावेज़ सेट करें।
{ "name": "signInWithPopup Demo", "manifest_version" 3, "background": { "service_worker": "background.js" }, "permissions": [ "offscreen" ] }
<!DOCTYPE html> <script src="./offscreen.js"></script>
// This URL must point to the public site const _URL = 'https://example.com/signInWithPopupExample'; const iframe = document.createElement('iframe'); iframe.src = _URL; document.documentElement.appendChild(iframe); chrome.runtime.onMessage.addListener(handleChromeMessages); function handleChromeMessages(message, sender, sendResponse) { // Extensions may have an number of other reasons to send messages, so you // should filter out any that are not meant for the offscreen document. if (message.target !== 'offscreen') { return false; } function handleIframeMessage({data}) { try { if (data.startsWith('!_{')) { // Other parts of the Firebase library send messages using postMessage. // You don't care about them in this context, so return early. return; } data = JSON.parse(data); self.removeEventListener('message', handleIframeMessage); sendResponse(data); } catch (e) { console.log(`json parse failed - ${e.message}`); } } globalThis.addEventListener('message', handleIframeMessage, false); // Initialize the authentication flow in the iframed document. You must set the // second argument (targetOrigin) of the message in order for it to be successfully // delivered. iframe.contentWindow.postMessage({"initAuth": true}, new URL(_URL).origin); return true; }
const OFFSCREEN_DOCUMENT_PATH = '/offscreen.html'; // A global promise to avoid concurrency issues let creatingOffscreenDocument; // Chrome only allows for a single offscreenDocument. This is a helper function // that returns a boolean indicating if a document is already active. async function hasDocument() { // Check all windows controlled by the service worker to see if one // of them is the offscreen document with the given path const matchedClients = await clients.matchAll(); return matchedClients.some( (c) => c.url === chrome.runtime.getURL(OFFSCREEN_DOCUMENT_PATH) ); } async function setupOffscreenDocument(path) { // If we do not have a document, we are already setup and can skip if (!(await hasDocument())) { // create offscreen document if (creating) { await creating; } else { creating = chrome.offscreen.createDocument({ url: path, reasons: [ chrome.offscreen.Reason.DOM_SCRAPING ], justification: 'authentication' }); await creating; creating = null; } } } async function closeOffscreenDocument() { if (!(await hasDocument())) { return; } await chrome.offscreen.closeDocument(); } function getAuth() { return new Promise(async (resolve, reject) => { const auth = await chrome.runtime.sendMessage({ type: 'firebase-auth', target: 'offscreen' }); auth?.name !== 'FirebaseError' ? resolve(auth) : reject(auth); }) } async function firebaseAuth() { await setupOffscreenDocument(OFFSCREEN_DOCUMENT_PATH); const auth = await getAuth() .then((auth) => { console.log('User Authenticated', auth); return auth; }) .catch(err => { if (err.code === 'auth/operation-not-allowed') { console.error('You must enable an OAuth provider in the Firebase' + ' console in order to use signInWithPopup. This sample' + ' uses Google by default.'); } else { console.error(err); return err; } }) .finally(closeOffscreenDocument) return auth; }
अब, जब आप अपने सेवा कर्मी के भीतर firebaseAuth()
कॉल करते हैं, तो यह ऑफस्क्रीन दस्तावेज़ बनाएगा और साइट को iframe में लोड करेगा। वह आईफ्रेम पृष्ठभूमि में संसाधित होगा, और फायरबेस मानक प्रमाणीकरण प्रवाह से गुजरेगा। एक बार जब इसे हल कर लिया गया या अस्वीकार कर दिया गया, तो ऑफस्क्रीन दस्तावेज़ का उपयोग करके प्रमाणीकरण ऑब्जेक्ट आपके आईफ्रेम से आपके सेवा कार्यकर्ता को भेज दिया जाएगा।