FirebaseUI के साथ अपने वेब ऐप में आसानी से साइन-इन जोड़ें

फायरबेसयूआई फायरबेस ऑथेंटिकेशन एसडीके के शीर्ष पर बनी एक लाइब्रेरी है जो आपके ऐप में उपयोग के लिए ड्रॉप-इन यूआई प्रवाह प्रदान करती है। FirebaseUI निम्नलिखित लाभ प्रदान करता है:

  • एकाधिक प्रदाता - ईमेल/पासवर्ड, ईमेल लिंक, फ़ोन प्रमाणीकरण, Google, Facebook, Twitter और GitHub साइन-इन के लिए साइन-इन प्रवाह।
  • खाता लिंकिंग - पहचान प्रदाताओं के बीच उपयोगकर्ता खातों को सुरक्षित रूप से जोड़ने के लिए प्रवाह।
  • अनुकूलन - आपकी ऐप आवश्यकताओं से मेल खाने के लिए फायरबेसयूआई की सीएसएस शैलियों को ओवरराइड करें। इसके अलावा, क्योंकि फायरबेसयूआई खुला स्रोत है, आप प्रोजेक्ट को फोर्क कर सकते हैं और इसे अपनी आवश्यकताओं के अनुसार अनुकूलित कर सकते हैं।
  • वन-टैप साइन-अप और स्वचालित साइन-इन - तेज़ क्रॉस-डिवाइस साइन-इन के लिए वन-टैप साइन-अप के साथ स्वचालित एकीकरण।
  • स्थानीयकृत यूआई - 40 से अधिक भाषाओं के लिए अंतर्राष्ट्रीयकरण।
  • अनाम उपयोगकर्ताओं को अपग्रेड करना - साइन-इन/साइन-अप के माध्यम से अनाम उपयोगकर्ताओं को अपग्रेड करने की क्षमता। अधिक जानकारी के लिए, अनाम उपयोगकर्ताओं को अपग्रेड करना अनुभाग पर जाएँ।

शुरू करने से पहले

  1. अपने वेब एप्लिकेशन में फायरबेस प्रमाणीकरण जोड़ें , यह सुनिश्चित करते हुए कि आप v9 कंपैट (अनुशंसित) या पुराने एसडीके (ऊपर साइडबार देखें) का उपयोग कर रहे हैं।

  2. निम्नलिखित विकल्पों में से किसी एक के माध्यम से FirebaseUI को शामिल करें:

    1. सीडीएन

      फायरबेस कंसोल से इनिशियलाइज़ेशन स्निपेट के नीचे, अपने पेज के <head> टैग में निम्नलिखित स्क्रिप्ट और CSS फ़ाइल शामिल करें:

      <script src="https://www.gstatic.com/firebasejs/ui/6.0.1/firebase-ui-auth.js"></script>
      <link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/6.0.1/firebase-ui-auth.css" />
      
    2. एनपीएम मॉड्यूल

      निम्नलिखित कमांड का उपयोग करके npm के माध्यम से FirebaseUI और उसकी निर्भरताएँ स्थापित करें:

      $ npm install firebaseui --save
      

      आपकी स्रोत फ़ाइलों में निम्नलिखित मॉड्यूल की require :

      var firebase = require('firebase');
      var firebaseui = require('firebaseui');
      
    3. बोवर घटक

      निम्नलिखित कमांड का उपयोग करके बोवर के माध्यम से फायरबेसयूआई और उसकी निर्भरता स्थापित करें:

      $ bower install firebaseui --save
      

      यदि आपका HTTP सर्वर bower_components/ के भीतर फ़ाइलें परोसता है, तो अपने HTML में आवश्यक फ़ाइलें शामिल करें:

      <script src="bower_components/firebaseui/dist/firebaseui.js"></script>
      <link type="text/css" rel="stylesheet" href="bower_components/firebaseui/dist/firebaseui.css" />
      

फायरबेसयूआई प्रारंभ करें

SDK आयात करने के बाद, प्रामाणिक UI प्रारंभ करें।

// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());

साइन-इन विधियाँ सेट करें

इससे पहले कि आप उपयोगकर्ताओं को साइन इन करने के लिए फायरबेस का उपयोग कर सकें, आपको उन साइन-इन विधियों को सक्षम और कॉन्फ़िगर करना होगा जिनका आप समर्थन करना चाहते हैं।

ईमेल पता और पासवर्ड

  1. फायरबेस कंसोल में, प्रमाणीकरण अनुभाग खोलें और ईमेल और पासवर्ड प्रमाणीकरण सक्षम करें।

  2. ईमेल प्रदाता आईडी को FirebaseUI signInOptions की सूची में जोड़ें।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        firebase.auth.EmailAuthProvider.PROVIDER_ID
      ],
      // Other config options...
    });
    
  3. वैकल्पिक : उपयोगकर्ता को एक प्रदर्शन नाम (डिफ़ॉल्ट रूप से true ) दर्ज करने की आवश्यकता के लिए EmailAuthProvider कॉन्फ़िगर किया जा सकता है।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        {
          provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
          requireDisplayName: false
        }
      ]
    });
    
  1. फायरबेस कंसोल में, प्रमाणीकरण अनुभाग खोलें। साइन इन विधि टैब पर, ईमेल/पासवर्ड प्रदाता को सक्षम करें। ध्यान दें कि ईमेल लिंक साइन-इन का उपयोग करने के लिए ईमेल/पासवर्ड साइन-इन सक्षम होना चाहिए।

  2. उसी अनुभाग में, ईमेल लिंक (पासवर्ड रहित साइन-इन) साइन-इन विधि सक्षम करें और सहेजें पर क्लिक करें।

  3. ईमेल प्रदाता आईडी को ईमेल लिंक signInMethod के साथ फायरबेसयूआई signInOptions की सूची में जोड़ें।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        {
          provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
          signInMethod: firebase.auth.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD
        }
      ],
      // Other config options...
    });
    
  4. साइन-इन यूआई को सशर्त रूप से प्रस्तुत करते समय (एकल पेज ऐप्स के लिए प्रासंगिक), यह पता लगाने के लिए ui.isPendingRedirect() का उपयोग करें कि क्या यूआरएल ईमेल लिंक के साथ साइन-इन से मेल खाता है और साइन-इन पूरा करने के लिए यूआई को प्रस्तुत करने की आवश्यकता है।

    // Is there an email link sign-in?
    if (ui.isPendingRedirect()) {
      ui.start('#firebaseui-auth-container', uiConfig);
    }
    // This can also be done via:
    if (firebase.auth().isSignInWithEmailLink(window.location.href)) {
      ui.start('#firebaseui-auth-container', uiConfig);
    }
    
  5. वैकल्पिक : ईमेल लिंक साइन-इन के लिए EmailAuthProvider उपयोगकर्ता को क्रॉस डिवाइस साइन-इन पूरा करने की अनुमति देने या ब्लॉक करने के लिए कॉन्फ़िगर किया जा सकता है।

    लिंक भेजते समय उपयोग करने के लिए firebase.auth.ActionCodeSettings कॉन्फ़िगरेशन को वापस करने के लिए एक वैकल्पिक emailLinkSignIn कॉलबैक को परिभाषित किया जा सकता है। यह निर्दिष्ट करने की क्षमता प्रदान करता है कि लिंक को कैसे प्रबंधित किया जा सकता है, कस्टम डायनेमिक लिंक, डीप लिंक में अतिरिक्त स्थिति, आदि। जब प्रदान नहीं किया जाता है, तो वर्तमान यूआरएल का उपयोग किया जाता है और केवल वेब प्रवाह ट्रिगर होता है।

    फायरबेसयूआई-वेब में ईमेल लिंक साइन-इन फायरबेसयूआई-एंड्रॉइड और फायरबेसयूआई-आईओएस के साथ संगत है, जहां फायरबेसयूआई-एंड्रॉइड से प्रवाह शुरू करने वाला एक उपयोगकर्ता लिंक खोल सकता है और फायरबेसयूआई-वेब के साथ साइन-इन पूरा कर सकता है। विपरीत प्रवाह के लिए भी यही सत्य है।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        {
          provider: firebase.auth.EmailAuthProvider.PROVIDER_ID,
          signInMethod: firebase.auth.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD,
          // Allow the user the ability to complete sign-in cross device,
          // including the mobile apps specified in the ActionCodeSettings
          // object below.
          forceSameDevice: false,
          // Used to define the optional firebase.auth.ActionCodeSettings if
          // additional state needs to be passed along request and whether to open
          // the link in a mobile app if it is installed.
          emailLinkSignIn: function() {
            return {
              // Additional state showPromo=1234 can be retrieved from URL on
              // sign-in completion in signInSuccess callback by checking
              // window.location.href.
              url: 'https://www.example.com/completeSignIn?showPromo=1234',
              // Custom FDL domain.
              dynamicLinkDomain: 'example.page.link',
              // Always true for email link sign-in.
              handleCodeInApp: true,
              // Whether to handle link in iOS app if installed.
              iOS: {
                bundleId: 'com.example.ios'
              },
              // Whether to handle link in Android app if opened in an Android
              // device.
              android: {
                packageName: 'com.example.android',
                installApp: true,
                minimumVersion: '12'
              }
            };
          }
        }
      ]
    });
    

OAuth प्रदाता (Google, Facebook, Twitter और GitHub)

  1. फायरबेस कंसोल में, प्रमाणीकरण अनुभाग खोलें और निर्दिष्ट OAuth प्रदाता साइन-इन सक्षम करें। सुनिश्चित करें कि संबंधित OAuth क्लाइंट आईडी और रहस्य भी निर्दिष्ट हैं।

  2. इसके अलावा प्रमाणीकरण अनुभाग में, सुनिश्चित करें कि जिस डोमेन पर आपका साइन-इन पृष्ठ प्रस्तुत किया जाएगा वह भी अधिकृत डोमेन सूची में जोड़ा गया है।

  3. OAuth प्रदाता आईडी को FirebaseUI signInOptions की सूची में जोड़ें।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        // List of OAuth providers supported.
        firebase.auth.GoogleAuthProvider.PROVIDER_ID,
        firebase.auth.FacebookAuthProvider.PROVIDER_ID,
        firebase.auth.TwitterAuthProvider.PROVIDER_ID,
        firebase.auth.GithubAuthProvider.PROVIDER_ID
      ],
      // Other config options...
    });
    
  4. वैकल्पिक : प्रति प्रदाता कस्टम स्कोप या कस्टम OAuth पैरामीटर निर्दिष्ट करने के लिए, आप केवल प्रदाता मान के बजाय एक ऑब्जेक्ट पास कर सकते हैं:

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        {
          provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
          scopes: [
            'https://www.googleapis.com/auth/contacts.readonly'
          ],
          customParameters: {
            // Forces account selection even when one account
            // is available.
            prompt: 'select_account'
          }
        },
        {
          provider: firebase.auth.FacebookAuthProvider.PROVIDER_ID,
          scopes: [
            'public_profile',
            'email',
            'user_likes',
            'user_friends'
          ],
          customParameters: {
            // Forces password re-entry.
            auth_type: 'reauthenticate'
          }
        },
        firebase.auth.TwitterAuthProvider.PROVIDER_ID, // Twitter does not support scopes.
        firebase.auth.EmailAuthProvider.PROVIDER_ID // Other providers don't need to be given as object.
      ]
    });
    

फ़ोन नंबर

  1. फायरबेस कंसोल में, प्रमाणीकरण अनुभाग खोलें और फ़ोन नंबर साइन-इन सक्षम करें।

  2. सुनिश्चित करें कि जिस डोमेन पर आपका साइन-इन पृष्ठ प्रस्तुत किया जाएगा वह भी अधिकृत डोमेन सूची में जोड़ा गया है।

  3. फ़ोन नंबर प्रदाता आईडी को FirebaseUI signInOptions की सूची में जोड़ें।

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        firebase.auth.PhoneAuthProvider.PROVIDER_ID
      ],
      // Other config options...
    });
    
  4. वैकल्पिक : PhoneAuthProvider को कस्टम reCAPTCHA पैरामीटर के साथ कॉन्फ़िगर किया जा सकता है, चाहे reCAPTCHA दृश्यमान हो या अदृश्य (सामान्य पर डिफ़ॉल्ट)। अधिक विवरण के लिए reCAPTCHA API दस्तावेज़ देखें।

    फ़ोन नंबर इनपुट में चयन करने के लिए डिफ़ॉल्ट देश भी सेट किया जा सकता है। कोड की पूरी सूची के लिए समर्थित देश कोड की सूची देखें। यदि अनिर्दिष्ट है, तो फ़ोन नंबर इनपुट संयुक्त राज्य अमेरिका (+1) पर डिफ़ॉल्ट होगा।

    वर्तमान में निम्नलिखित विकल्प समर्थित हैं.

    ui.start('#firebaseui-auth-container', {
      signInOptions: [
        {
          provider: firebase.auth.PhoneAuthProvider.PROVIDER_ID,
          recaptchaParameters: {
            type: 'image', // 'audio'
            size: 'normal', // 'invisible' or 'compact'
            badge: 'bottomleft' //' bottomright' or 'inline' applies to invisible.
          },
          defaultCountry: 'GB', // Set default country to the United Kingdom (+44).
          // For prefilling the national number, set defaultNationNumber.
          // This will only be observed if only phone Auth provider is used since
          // for multiple providers, the NASCAR screen will always render first
          // with a 'sign in with phone number' button.
          defaultNationalNumber: '1234567890',
          // You can also pass the full phone number string instead of the
          // 'defaultCountry' and 'defaultNationalNumber'. However, in this case,
          // the first country ID that matches the country code will be used to
          // populate the country selector. So for countries that share the same
          // country code, the selected country may not be the expected one.
          // In that case, pass the 'defaultCountry' instead to ensure the exact
          // country is selected. The 'defaultCountry' and 'defaultNationaNumber'
          // will always have higher priority than 'loginHint' which will be ignored
          // in their favor. In this case, the default country will be 'GB' even
          // though 'loginHint' specified the country code as '+1'.
          loginHint: '+11234567890'
        }
      ]
    });
    

दाखिल करना

FirebaseUI साइन इन फ़्लो शुरू करने के लिए, अंतर्निहित Auth इंस्टेंस को पास करके FirebaseUI इंस्टेंस प्रारंभ करें।

// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());

HTML तत्व को परिभाषित करें जहां FirebaseUI साइन-इन विजेट प्रस्तुत किया जाएगा।

<!-- The surrounding HTML is left untouched by FirebaseUI.
     Your app may use that space for branding, controls and other customizations.-->
<h1>Welcome to My Awesome App</h1>
<div id="firebaseui-auth-container"></div>
<div id="loader">Loading...</div>

फायरबेसयूआई कॉन्फ़िगरेशन निर्दिष्ट करें (प्रदाता समर्थित और यूआई अनुकूलन के साथ-साथ सफलता कॉलबैक इत्यादि)।

var uiConfig = {
  callbacks: {
    signInSuccessWithAuthResult: function(authResult, redirectUrl) {
      // User successfully signed in.
      // Return type determines whether we continue the redirect automatically
      // or whether we leave that to developer to handle.
      return true;
    },
    uiShown: function() {
      // The widget is rendered.
      // Hide the loader.
      document.getElementById('loader').style.display = 'none';
    }
  },
  // Will use popup for IDP Providers sign-in flow instead of the default, redirect.
  signInFlow: 'popup',
  signInSuccessUrl: '<url-to-redirect-to-on-success>',
  signInOptions: [
    // Leave the lines as is for the providers you want to offer your users.
    firebase.auth.GoogleAuthProvider.PROVIDER_ID,
    firebase.auth.FacebookAuthProvider.PROVIDER_ID,
    firebase.auth.TwitterAuthProvider.PROVIDER_ID,
    firebase.auth.GithubAuthProvider.PROVIDER_ID,
    firebase.auth.EmailAuthProvider.PROVIDER_ID,
    firebase.auth.PhoneAuthProvider.PROVIDER_ID
  ],
  // Terms of service url.
  tosUrl: '<your-tos-url>',
  // Privacy policy url.
  privacyPolicyUrl: '<your-privacy-policy-url>'
};

अंत में, FirebaseUI प्रामाणिक इंटरफ़ेस प्रस्तुत करें:

// The start method will wait until the DOM is loaded.
ui.start('#firebaseui-auth-container', uiConfig);

अनाम उपयोगकर्ताओं को अपग्रेड करना

अनाम उपयोगकर्ता उन्नयन सक्षम करना

जब कोई अनाम उपयोगकर्ता साइन इन करता है या स्थायी खाते से साइन अप करता है, तो आप यह सुनिश्चित करना चाहते हैं कि उपयोगकर्ता साइन अप करने से पहले जो कर रहा था उसे जारी रख सके। ऐसा करने के लिए, जब आप साइन-इन यूआई कॉन्फ़िगर करते हैं तो बस autoUpgradeAnonymousUsers true पर सेट करें (यह विकल्प डिफ़ॉल्ट रूप से अक्षम है)।

अनाम उपयोगकर्ता अपग्रेड मर्ज विवादों को संभालना

ऐसे मामले होते हैं जब कोई उपयोगकर्ता, शुरू में गुमनाम रूप से साइन इन करता है, मौजूदा फायरबेस उपयोगकर्ता में अपग्रेड करने का प्रयास करता है। चूँकि एक मौजूदा उपयोगकर्ता को किसी अन्य मौजूदा उपयोगकर्ता से नहीं जोड़ा जा सकता है, उपरोक्त होने पर FirebaseUI एक त्रुटि कोड firebaseui/anonymous-upgrade-merge-conflict के साथ signInFailure कॉलबैक को ट्रिगर करेगा। त्रुटि ऑब्जेक्ट में स्थायी क्रेडेंशियल भी शामिल होगा। साइन-इन पूरा करने के लिए कॉलबैक में स्थायी क्रेडेंशियल के साथ साइन-इन ट्रिगर किया जाना चाहिए। इससे पहले कि साइन-इन auth.signInWithCredential(error.credential) के माध्यम से पूरा किया जा सके, आपको अनाम उपयोगकर्ता का डेटा सहेजना होगा और अनाम उपयोगकर्ता को हटाना होगा। फिर, साइन-इन पूरा होने के बाद, डेटा को गैर-अनाम उपयोगकर्ता के पास वापस कॉपी करें। नीचे दिया गया एक उदाहरण बताता है कि यह प्रवाह कैसे काम करेगा।

// Temp variable to hold the anonymous user data if needed.
var data = null;
// Hold a reference to the anonymous current user.
var anonymousUser = firebase.auth().currentUser;
ui.start('#firebaseui-auth-container', {
  // Whether to upgrade anonymous users should be explicitly provided.
  // The user must already be signed in anonymously before FirebaseUI is
  // rendered.
  autoUpgradeAnonymousUsers: true,
  signInSuccessUrl: '<url-to-redirect-to-on-success>',
  signInOptions: [
    firebase.auth.GoogleAuthProvider.PROVIDER_ID,
    firebase.auth.FacebookAuthProvider.PROVIDER_ID,
    firebase.auth.EmailAuthProvider.PROVIDER_ID,
    firebase.auth.PhoneAuthProvider.PROVIDER_ID
  ],
  callbacks: {
    // signInFailure callback must be provided to handle merge conflicts which
    // occur when an existing credential is linked to an anonymous user.
    signInFailure: function(error) {
      // For merge conflicts, the error.code will be
      // 'firebaseui/anonymous-upgrade-merge-conflict'.
      if (error.code != 'firebaseui/anonymous-upgrade-merge-conflict') {
        return Promise.resolve();
      }
      // The credential the user tried to sign in with.
      var cred = error.credential;
      // Copy data from anonymous user to permanent user and delete anonymous
      // user.
      // ...
      // Finish sign-in after data is copied.
      return firebase.auth().signInWithCredential(cred);
    }
  }
});

अगले कदम

  • FirebaseUI के उपयोग और अनुकूलन के बारे में अधिक जानकारी के लिए, README पर जाएँ।
  • यदि आपको FirebaseUI में कोई समस्या मिलती है और आप इसकी रिपोर्ट करना चाहते हैं, तो GitHub समस्या ट्रैकर का उपयोग करें।