अगर आपने Firebase Authentication with Identity Platform पर अपग्रेड किया है, तो अपने वेब ऐप्लिकेशन में एसएमएस के ज़रिए दो चरणों में पुष्टि करने की सुविधा जोड़ी जा सकती है.
बहु-स्तरीय पुष्टि (एमएफ़ए) की सुविधा से, आपके ऐप्लिकेशन की सुरक्षा बढ़ जाती है. हमलावर अक्सर पासवर्ड और सोशल मीडिया खातों को हैक कर लेते हैं. हालांकि, टेक्स्ट मैसेज को इंटरसेप्ट करना ज़्यादा मुश्किल होता है.
शुरू करने से पहले
कई चरणों में पुष्टि करने की सुविधा देने वाली कम से कम एक सेवा चालू करें. फ़ोन से पुष्टि करने, पहचान छिपाकर पुष्टि करने, और Apple Game Center को छोड़कर, हर सुविधा देने वाली कंपनी एमएफ़ए की सुविधा देती है.
उन देशों/इलाकों के लिए एसएमएस से पुष्टि करने की सुविधा चालू करें जहां आपको इसका इस्तेमाल करना है. Firebase, एसएमएस भेजने के लिए पूरी तरह से ब्लॉक करने वाली क्षेत्र की नीति का इस्तेमाल करता है. इससे आपके प्रोजेक्ट डिफ़ॉल्ट रूप से ज़्यादा सुरक्षित हो जाते हैं.
पक्का करें कि आपका ऐप्लिकेशन, उपयोगकर्ता के ईमेल की पुष्टि कर रहा हो. एमएफ़ए के लिए, ईमेल पते की पुष्टि करना ज़रूरी है. इससे नुकसान पहुंचाने वाले लोगों या इकाइयों को, ऐसे ईमेल पते से किसी सेवा के लिए रजिस्टर करने से रोका जा सकता है जिसका मालिकाना हक उनके पास नहीं है. इसके बाद, वे पुष्टि करने का दूसरा तरीका जोड़कर, असली मालिक को खाते से लॉक आउट कर सकते हैं.
एक से ज़्यादा किरायेदार वाली सुविधा का इस्तेमाल करना
अगर आपको मल्टी-टेनेंट एनवायरमेंट में इस्तेमाल करने के लिए, कई चरणों में पुष्टि करने की सुविधा चालू करनी है, तो पक्का करें कि आपने यहां दिया गया तरीका पूरा कर लिया हो. साथ ही, इस दस्तावेज़ में दिए गए बाकी निर्देशों का पालन करें:
Google Cloud Console में, वह किरायेदार चुनें जिसके साथ आपको काम करना है.
अपने कोड में,
Authइंस्टेंस परtenantIdफ़ील्ड को अपने किरायेदार के आईडी पर सेट करें. उदाहरण के लिए:Web
import { getAuth } from "firebase/auth"; const auth = getAuth(app); auth.tenantId = "myTenantId1";Web
firebase.auth().tenantId = 'myTenantId1';
कई चरणों में पुष्टि करने की सुविधा चालू करना
Firebase कंसोल के Authentication > Sign-in method पेज पर जाएं.
ऐडवांस सेक्शन में जाकर, एसएमएस के ज़रिए कई चरणों में पुष्टि करने की सुविधा चालू करें.
आपको उन फ़ोन नंबरों को भी डालना चाहिए जिनसे आपको अपने ऐप्लिकेशन की जांच करनी है. टेस्ट फ़ोन नंबर रजिस्टर करना ज़रूरी नहीं है. हालांकि, हम इसका सुझाव देते हैं, ताकि डेवलपमेंट के दौरान थ्रॉटलिंग से बचा जा सके.
अगर आपने अपने ऐप्लिकेशन के डोमेन को पहले से अनुमति नहीं दी है, तो उसे Firebase कंसोल के पुष्टि > सेटिंग पेज पर, अनुमति वाली सूची में जोड़ें.
पंजीकरण का पैटर्न चुनना
आपके पास यह चुनने का विकल्प होता है कि आपके ऐप्लिकेशन के लिए, बहु-स्तरीय पुष्टि (एमएफ़ए) की ज़रूरत है या नहीं. साथ ही, यह भी तय किया जा सकता है कि उपयोगकर्ताओं को कब और कैसे रजिस्टर करना है. कुछ सामान्य पैटर्न में ये शामिल हैं:
रजिस्ट्रेशन के दौरान, उपयोगकर्ता के दूसरे फ़ैक्टर को रजिस्टर करें. अगर आपके ऐप्लिकेशन में सभी उपयोगकर्ताओं के लिए, कई चरणों में पुष्टि करने की सुविधा ज़रूरी है, तो इस तरीके का इस्तेमाल करें.
रजिस्ट्रेशन के दौरान, पुष्टि के दूसरे तरीके के लिए ऑप्ट-इन करने का विकल्प दें. इस विकल्प को स्किप किया जा सकता है. जिन ऐप्लिकेशन को कई चरणों में पुष्टि करने की सुविधा को बढ़ावा देना है, लेकिन इसे ज़रूरी नहीं बनाना है वे इस तरीके का इस्तेमाल कर सकते हैं.
साइन अप स्क्रीन के बजाय, उपयोगकर्ता के खाते या प्रोफ़ाइल मैनेजमेंट पेज से दूसरा फ़ैक्टर जोड़ने की सुविधा दें. इससे रजिस्ट्रेशन प्रोसेस के दौरान आने वाली समस्याओं को कम किया जा सकता है. साथ ही, सुरक्षा के लिहाज़ से संवेदनशील उपयोगकर्ताओं के लिए, मल्टी-फ़ैक्टर ऑथेंटिकेशन की सुविधा उपलब्ध कराई जा सकती है.
जब उपयोगकर्ता को ज़्यादा सुरक्षा वाली सुविधाओं को ऐक्सेस करना हो, तब पुष्टि करने के दूसरे तरीके को धीरे-धीरे जोड़ने की ज़रूरत होती है.
reCAPTCHA की पुष्टि करने वाले टूल को सेट अप करना
एसएमएस कोड भेजने से पहले, आपको reCAPTCHA की पुष्टि करने वाला टूल कॉन्फ़िगर करना होगा. Firebase, reCAPTCHA का इस्तेमाल करता है, ताकि यह पक्का किया जा सके कि फ़ोन नंबर की पुष्टि करने के अनुरोध, आपके ऐप्लिकेशन के उन डोमेन से आएं जिन्हें अनुमति दी गई है. इससे, गलत इस्तेमाल को रोका जा सकता है.
आपको मैन्युअल तरीके से reCAPTCHA क्लाइंट सेट अप करने की ज़रूरत नहीं है. क्लाइंट SDK का RecaptchaVerifier ऑब्जेक्ट, ज़रूरी क्लाइंट कुंजियां और सीक्रेट अपने-आप बनाता है और उन्हें शुरू करता है.
दिखाई न देने वाले reCAPTCHA का इस्तेमाल करना
RecaptchaVerifier ऑब्जेक्ट, इनविज़िबल reCAPTCHA के साथ काम करता है. यह अक्सर उपयोगकर्ता की पुष्टि कर सकता है. इसके लिए, किसी इंटरैक्शन की ज़रूरत नहीं होती. अदृश्य reCAPTCHA का इस्तेमाल करने के लिए, RecaptchaVerifier बनाएं. साथ ही, size पैरामीटर को invisible पर सेट करें. इसके अलावा, यूज़र इंटरफ़ेस (यूआई) एलिमेंट का आईडी डालें, जो मल्टी-फ़ैक्टर ऑथेंटिकेशन के लिए रजिस्टर करने की प्रोसेस शुरू करता है:
Web
import { RecaptchaVerifier, getAuth } from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier(getAuth(), "sign-in-button", {
"size": "invisible",
"callback": function(response) {
// reCAPTCHA solved, you can proceed with
// phoneAuthProvider.verifyPhoneNumber(...).
onSolvedRecaptcha();
}
});
Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
'size': 'invisible',
'callback': function(response) {
// reCAPTCHA solved, you can proceed with phoneAuthProvider.verifyPhoneNumber(...).
onSolvedRecaptcha();
}
});
reCAPTCHA विजेट का इस्तेमाल करना
दिखने वाले reCAPTCHA विजेट का इस्तेमाल करने के लिए, विजेट को शामिल करने के लिए एक एचटीएमएल एलिमेंट बनाएं. इसके बाद, यूज़र इंटरफ़ेस (यूआई) कंटेनर के आईडी के साथ एक RecaptchaVerifier ऑब्जेक्ट बनाएं. आपके पास कॉल बैक सेट करने का विकल्प भी होता है. ये कॉल बैक तब शुरू होते हैं, जब reCAPTCHA को हल कर लिया जाता है या उसकी समयसीमा खत्म हो जाती है:
Web
import { RecaptchaVerifier, getAuth } from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier(
getAuth(),
"recaptcha-container",
// Optional reCAPTCHA parameters.
{
"size": "normal",
"callback": function(response) {
// reCAPTCHA solved, you can proceed with
// phoneAuthProvider.verifyPhoneNumber(...).
onSolvedRecaptcha();
},
"expired-callback": function() {
// Response expired. Ask user to solve reCAPTCHA again.
// ...
}
}
);
Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
'recaptcha-container',
// Optional reCAPTCHA parameters.
{
'size': 'normal',
'callback': function(response) {
// reCAPTCHA solved, you can proceed with phoneAuthProvider.verifyPhoneNumber(...).
// ...
onSolvedRecaptcha();
},
'expired-callback': function() {
// Response expired. Ask user to solve reCAPTCHA again.
// ...
}
});
reCAPTCHA को पहले से रेंडर करना
वैकल्पिक तौर पर, दो चरणों में पुष्टि करने की सुविधा के लिए रजिस्टर करने से पहले, reCAPTCHA को पहले से रेंडर किया जा सकता है:
Web
recaptchaVerifier.render()
.then(function (widgetId) {
window.recaptchaWidgetId = widgetId;
});
Web
recaptchaVerifier.render()
.then(function(widgetId) {
window.recaptchaWidgetId = widgetId;
});
render() हल होने के बाद, आपको reCAPTCHA का विजेट आईडी मिलता है. इसका इस्तेमाल करके, reCAPTCHA API को कॉल किया जा सकता है:
var recaptchaResponse = grecaptcha.getResponse(window.recaptchaWidgetId);
RecaptchaVerifier, verify तरीके से इस लॉजिक को अलग कर देता है. इसलिए, आपको grecaptcha वैरिएबल को सीधे तौर पर हैंडल करने की ज़रूरत नहीं होती.
पुष्टि करने का दूसरा तरीका रजिस्टर करना
किसी उपयोगकर्ता के लिए, पुष्टि करने का नया सेकंडरी तरीका रजिस्टर करने के लिए:
उपयोगकर्ता की फिर से पुष्टि करें.
उपयोगकर्ता से उसका फ़ोन नंबर डालने के लिए कहें.
पिछले सेक्शन में बताए गए तरीके से, reCAPTCHA की पुष्टि करने वाले टूल को शुरू करें. अगर RecaptchaVerifier इंस्टेंस पहले से कॉन्फ़िगर है, तो इस चरण को छोड़ दें:
Web
import { RecaptchaVerifier, getAuth } from "firebase/auth"; const recaptchaVerifier = new RecaptchaVerifier( getAuth(),'recaptcha-container-id', undefined);Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container-id');उपयोगकर्ता के लिए मल्टी-फ़ैक्टर सेशन पाएं:
Web
import { multiFactor } from "firebase/auth"; multiFactor(user).getSession().then(function (multiFactorSession) { // ... });Web
user.multiFactor.getSession().then(function(multiFactorSession) { // ... })उपयोगकर्ता के फ़ोन नंबर और मल्टी-फ़ैक्टर सेशन के साथ
PhoneInfoOptionsऑब्जेक्ट को शुरू करें:Web
// Specify the phone number and pass the MFA session. const phoneInfoOptions = { phoneNumber: phoneNumber, session: multiFactorSession };Web
// Specify the phone number and pass the MFA session. var phoneInfoOptions = { phoneNumber: phoneNumber, session: multiFactorSession };उपयोगकर्ता के फ़ोन पर पुष्टि करने के लिए मैसेज भेजें:
Web
import { PhoneAuthProvider } from "firebase/auth"; const phoneAuthProvider = new PhoneAuthProvider(auth); phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier) .then(function (verificationId) { // verificationId will be needed to complete enrollment. });Web
var phoneAuthProvider = new firebase.auth.PhoneAuthProvider(); // Send SMS verification code. return phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier) .then(function(verificationId) { // verificationId will be needed for enrollment completion. })हालांकि, ऐसा करना ज़रूरी नहीं है, लेकिन उपयोगकर्ताओं को पहले से यह जानकारी देना सबसे सही तरीका है कि उन्हें एक एसएमएस मिलेगा और उन पर सामान्य शुल्क लागू होंगे.
अगर अनुरोध पूरा नहीं होता है, तो reCAPTCHA को रीसेट करें. इसके बाद, पिछले चरण को दोहराएं, ताकि उपयोगकर्ता फिर से कोशिश कर सके. ध्यान दें कि
verifyPhoneNumber(), गड़बड़ी होने पर reCAPTCHA को अपने-आप रीसेट कर देगा. ऐसा इसलिए, क्योंकि reCAPTCHA टोकन का इस्तेमाल सिर्फ़ एक बार किया जा सकता है.Web
recaptchaVerifier.clear();Web
recaptchaVerifier.clear();एसएमएस कोड भेजने के बाद, उपयोगकर्ता से कोड की पुष्टि करने के लिए कहें:
Web
// Ask user for the verification code. Then: const cred = PhoneAuthProvider.credential(verificationId, verificationCode);Web
// Ask user for the verification code. Then: var cred = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);PhoneAuthCredentialका इस्तेमाल करके,MultiFactorAssertionऑब्जेक्ट शुरू करें:Web
import { PhoneMultiFactorGenerator } from "firebase/auth"; const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(cred);Web
var multiFactorAssertion = firebase.auth.PhoneMultiFactorGenerator.assertion(cred);पंजीकरण की प्रोसेस पूरी करें. विकल्प के तौर पर, दूसरे फ़ैक्टर के लिए डिसप्ले नेम तय किया जा सकता है. यह उन लोगों के लिए फ़ायदेमंद है जो पुष्टि करने के लिए एक से ज़्यादा तरीकों का इस्तेमाल करते हैं. ऐसा इसलिए, क्योंकि पुष्टि करने की प्रोसेस के दौरान फ़ोन नंबर छिपा दिया जाता है. उदाहरण के लिए, +1******1234.
Web
// Complete enrollment. This will update the underlying tokens // and trigger ID token change listener. multiFactor(user).enroll(multiFactorAssertion, "My personal phone number");Web
// Complete enrollment. This will update the underlying tokens // and trigger ID token change listener. user.multiFactor.enroll(multiFactorAssertion, 'My personal phone number');
नीचे दिए गए कोड में, दूसरे फ़ैक्टर के लिए रजिस्टर करने का पूरा उदाहरण दिखाया गया है:
Web
import {
multiFactor, PhoneAuthProvider, PhoneMultiFactorGenerator,
RecaptchaVerifier, getAuth
} from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier(getAuth(),
'recaptcha-container-id', undefined);
multiFactor(user).getSession()
.then(function (multiFactorSession) {
// Specify the phone number and pass the MFA session.
const phoneInfoOptions = {
phoneNumber: phoneNumber,
session: multiFactorSession
};
const phoneAuthProvider = new PhoneAuthProvider(auth);
// Send SMS verification code.
return phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier);
}).then(function (verificationId) {
// Ask user for the verification code. Then:
const cred = PhoneAuthProvider.credential(verificationId, verificationCode);
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(cred);
// Complete enrollment.
return multiFactor(user).enroll(multiFactorAssertion, mfaDisplayName);
});
Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container-id');
user.multiFactor.getSession().then(function(multiFactorSession) {
// Specify the phone number and pass the MFA session.
var phoneInfoOptions = {
phoneNumber: phoneNumber,
session: multiFactorSession
};
var phoneAuthProvider = new firebase.auth.PhoneAuthProvider();
// Send SMS verification code.
return phoneAuthProvider.verifyPhoneNumber(
phoneInfoOptions, recaptchaVerifier);
})
.then(function(verificationId) {
// Ask user for the verification code.
var cred = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);
var multiFactorAssertion = firebase.auth.PhoneMultiFactorGenerator.assertion(cred);
// Complete enrollment.
return user.multiFactor.enroll(multiFactorAssertion, mfaDisplayName);
});
बधाई हो! आपने किसी उपयोगकर्ता के लिए, पुष्टि करने का दूसरा तरीका रजिस्टर कर लिया है.
उपयोगकर्ताओं को दूसरे फ़ैक्टर से साइन इन करना
एसएमएस के ज़रिए दो फ़ैक्टर से पुष्टि करने की सुविधा का इस्तेमाल करके किसी उपयोगकर्ता को साइन इन करने के लिए:
उपयोगकर्ता को पहले फ़ैक्टर से साइन इन करें. इसके बाद,
auth/multi-factor-auth-requiredगड़बड़ी को ठीक करें. इस गड़बड़ी में एक रिज़ॉल्वर, रजिस्टर किए गए दूसरे फ़ैक्टर के बारे में जानकारी, और एक सेशन होता है. इससे यह पता चलता है कि उपयोगकर्ता ने पहले फ़ैक्टर से पुष्टि कर ली है.उदाहरण के लिए, अगर उपयोगकर्ता का पहला फ़ैक्टर ईमेल और पासवर्ड था, तो:
Web
import { getAuth, signInWithEmailAndPassword, getMultiFactorResolver} from "firebase/auth"; const auth = getAuth(); signInWithEmailAndPassword(auth, email, password) .then(function (userCredential) { // User successfully signed in and is not enrolled with a second factor. }) .catch(function (error) { if (error.code == 'auth/multi-factor-auth-required') { // The user is a multi-factor user. Second factor challenge is required. resolver = getMultiFactorResolver(auth, error); // ... } else if (error.code == 'auth/wrong-password') { // Handle other errors such as wrong password. } });Web
firebase.auth().signInWithEmailAndPassword(email, password) .then(function(userCredential) { // User successfully signed in and is not enrolled with a second factor. }) .catch(function(error) { if (error.code == 'auth/multi-factor-auth-required') { // The user is a multi-factor user. Second factor challenge is required. resolver = error.resolver; // ... } else if (error.code == 'auth/wrong-password') { // Handle other errors such as wrong password. } ... });अगर उपयोगकर्ता का पहला फ़ैक्टर, फ़ेडरेटेड प्रोवाइडर है, जैसे कि OAuth, SAML या OIDC, तो
signInWithPopup()याsignInWithRedirect()को कॉल करने के बाद गड़बड़ी को ठीक करें.अगर उपयोगकर्ता ने पुष्टि के लिए एक से ज़्यादा सेकंडरी तरीके रजिस्टर किए हैं, तो उससे पूछें कि उसे इनमें से कौनसे तरीके का इस्तेमाल करना है:
Web
// Ask user which second factor to use. // You can get the masked phone number via resolver.hints[selectedIndex].phoneNumber // You can get the display name via resolver.hints[selectedIndex].displayName if (resolver.hints[selectedIndex].factorId === PhoneMultiFactorGenerator.FACTOR_ID) { // User selected a phone second factor. // ... } else if (resolver.hints[selectedIndex].factorId === TotpMultiFactorGenerator.FACTOR_ID) { // User selected a TOTP second factor. // ... } else { // Unsupported second factor. }Web
// Ask user which second factor to use. // You can get the masked phone number via resolver.hints[selectedIndex].phoneNumber // You can get the display name via resolver.hints[selectedIndex].displayName if (resolver.hints[selectedIndex].factorId === firebase.auth.PhoneMultiFactorGenerator.FACTOR_ID) { // User selected a phone second factor. // ... } else if (resolver.hints[selectedIndex].factorId === firebase.auth.TotpMultiFactorGenerator.FACTOR_ID) { // User selected a TOTP second factor. // ... } else { // Unsupported second factor. }पिछले सेक्शन में बताए गए तरीके से, reCAPTCHA की पुष्टि करने वाले टूल को शुरू करें. अगर RecaptchaVerifier इंस्टेंस पहले से कॉन्फ़िगर है, तो इस चरण को छोड़ दें:
Web
import { RecaptchaVerifier, getAuth } from "firebase/auth"; recaptchaVerifier = new RecaptchaVerifier(getAuth(), 'recaptcha-container-id', undefined);Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container-id');उपयोगकर्ता के फ़ोन नंबर और मल्टी-फ़ैक्टर सेशन के साथ
PhoneInfoOptionsऑब्जेक्ट को शुरू करें. ये वैल्यू,resolverऑब्जेक्ट में शामिल होती हैं. इस ऑब्जेक्ट कोauth/multi-factor-auth-requirederror में पास किया जाता है:Web
const phoneInfoOptions = { multiFactorHint: resolver.hints[selectedIndex], session: resolver.session };Web
var phoneInfoOptions = { multiFactorHint: resolver.hints[selectedIndex], session: resolver.session };उपयोगकर्ता के फ़ोन पर पुष्टि करने के लिए मैसेज भेजें:
Web
// Send SMS verification code. const phoneAuthProvider = new PhoneAuthProvider(auth); phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier) .then(function (verificationId) { // verificationId will be needed for sign-in completion. });Web
var phoneAuthProvider = new firebase.auth.PhoneAuthProvider(); // Send SMS verification code. return phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier) .then(function(verificationId) { // verificationId will be needed for sign-in completion. })अगर अनुरोध पूरा नहीं होता है, तो reCAPTCHA को रीसेट करें. इसके बाद, पिछले चरण को दोहराएं, ताकि उपयोगकर्ता फिर से कोशिश कर सके:
Web
recaptchaVerifier.clear();Web
recaptchaVerifier.clear();एसएमएस कोड भेजने के बाद, उपयोगकर्ता से कोड की पुष्टि करने के लिए कहें:
Web
const cred = PhoneAuthProvider.credential(verificationId, verificationCode);Web
// Ask user for the verification code. Then: var cred = firebase.auth.PhoneAuthProvider.credential(verificationId, verificationCode);PhoneAuthCredentialका इस्तेमाल करके,MultiFactorAssertionऑब्जेक्ट शुरू करें:Web
const multiFactorAssertion = PhoneMultiFactorGenerator.assertion(cred);Web
var multiFactorAssertion = firebase.auth.PhoneMultiFactorGenerator.assertion(cred);दूसरे तरीके से पुष्टि करने की प्रोसेस पूरी करने के लिए,
resolver.resolveSignIn()पर कॉल करें. इसके बाद, आपको साइन-इन के ओरिजनल नतीजे का ऐक्सेस मिल जाएगा. इसमें स्टैंडर्ड प्रोवाइडर के हिसाब से डेटा और पुष्टि करने के क्रेडेंशियल शामिल होते हैं:Web
// Complete sign-in. This will also trigger the Auth state listeners. resolver.resolveSignIn(multiFactorAssertion) .then(function (userCredential) { // userCredential will also contain the user, additionalUserInfo, optional // credential (null for email/password) associated with the first factor sign-in. // For example, if the user signed in with Google as a first factor, // userCredential.additionalUserInfo will contain data related to Google // provider that the user signed in with. // - user.credential contains the Google OAuth credential. // - user.credential.accessToken contains the Google OAuth access token. // - user.credential.idToken contains the Google OAuth ID token. });Web
// Complete sign-in. This will also trigger the Auth state listeners. resolver.resolveSignIn(multiFactorAssertion) .then(function(userCredential) { // userCredential will also contain the user, additionalUserInfo, optional // credential (null for email/password) associated with the first factor sign-in. // For example, if the user signed in with Google as a first factor, // userCredential.additionalUserInfo will contain data related to Google provider that // the user signed in with. // user.credential contains the Google OAuth credential. // user.credential.accessToken contains the Google OAuth access token. // user.credential.idToken contains the Google OAuth ID token. });
नीचे दिए गए कोड में, मल्टी-फ़ैक्टर ऑथेंटिकेशन का इस्तेमाल करने वाले उपयोगकर्ता के साइन इन करने का पूरा उदाहरण दिखाया गया है:
Web
import {
getAuth,
getMultiFactorResolver,
PhoneAuthProvider,
PhoneMultiFactorGenerator,
RecaptchaVerifier,
signInWithEmailAndPassword
} from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier(getAuth(),
'recaptcha-container-id', undefined);
const auth = getAuth();
signInWithEmailAndPassword(auth, email, password)
.then(function (userCredential) {
// User is not enrolled with a second factor and is successfully
// signed in.
// ...
})
.catch(function (error) {
if (error.code == 'auth/multi-factor-auth-required') {
const resolver = getMultiFactorResolver(auth, error);
// Ask user which second factor to use.
if (resolver.hints[selectedIndex].factorId ===
PhoneMultiFactorGenerator.FACTOR_ID) {
const phoneInfoOptions = {
multiFactorHint: resolver.hints[selectedIndex],
session: resolver.session
};
const phoneAuthProvider = new PhoneAuthProvider(auth);
// Send SMS verification code
return phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier)
.then(function (verificationId) {
// Ask user for the SMS verification code. Then:
const cred = PhoneAuthProvider.credential(
verificationId, verificationCode);
const multiFactorAssertion =
PhoneMultiFactorGenerator.assertion(cred);
// Complete sign-in.
return resolver.resolveSignIn(multiFactorAssertion)
})
.then(function (userCredential) {
// User successfully signed in with the second factor phone number.
});
} else if (resolver.hints[selectedIndex].factorId ===
TotpMultiFactorGenerator.FACTOR_ID) {
// Handle TOTP MFA.
// ...
} else {
// Unsupported second factor.
}
} else if (error.code == 'auth/wrong-password') {
// Handle other errors such as wrong password.
}
});
Web
var resolver;
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function(userCredential) {
// User is not enrolled with a second factor and is successfully signed in.
// ...
})
.catch(function(error) {
if (error.code == 'auth/multi-factor-auth-required') {
resolver = error.resolver;
// Ask user which second factor to use.
if (resolver.hints[selectedIndex].factorId ===
firebase.auth.PhoneMultiFactorGenerator.FACTOR_ID) {
var phoneInfoOptions = {
multiFactorHint: resolver.hints[selectedIndex],
session: resolver.session
};
var phoneAuthProvider = new firebase.auth.PhoneAuthProvider();
// Send SMS verification code
return phoneAuthProvider.verifyPhoneNumber(phoneInfoOptions, recaptchaVerifier)
.then(function(verificationId) {
// Ask user for the SMS verification code.
var cred = firebase.auth.PhoneAuthProvider.credential(
verificationId, verificationCode);
var multiFactorAssertion =
firebase.auth.PhoneMultiFactorGenerator.assertion(cred);
// Complete sign-in.
return resolver.resolveSignIn(multiFactorAssertion)
})
.then(function(userCredential) {
// User successfully signed in with the second factor phone number.
});
} else if (resolver.hints[selectedIndex].factorId ===
firebase.auth.TotpMultiFactorGenerator.FACTOR_ID) {
// Handle TOTP MFA.
// ...
} else {
// Unsupported second factor.
}
} else if (error.code == 'auth/wrong-password') {
// Handle other errors such as wrong password.
} ...
});
बधाई हो! आपने बहु-फ़ैक्टर पुष्टि का इस्तेमाल करके, किसी उपयोगकर्ता के खाते में सफलतापूर्वक साइन इन किया है.
आगे क्या करना है
- Admin SDK की मदद से, प्रोग्राम के हिसाब से मल्टी-फ़ैक्टर ऑथेंटिकेशन का इस्तेमाल करने वाले लोगों को मैनेज करें.