अगर आपने Identity Platform के साथ Firebase से पुष्टि करने की सुविधा पर अपग्रेड किया है, तो आपके पास अपने वेब ऐप्लिकेशन में एसएमएस की मदद से पुष्टि करने की सुविधा जोड़ने का विकल्प है.
बहु-स्तरीय पुष्टि (MFA) की सुविधा से आपके ऐप्लिकेशन की सुरक्षा बढ़ जाती है. हालांकि, हमलावर अक्सर पासवर्ड और सोशल मीडिया खातों के साथ छेड़छाड़ करते हैं, लेकिन टेक्स्ट मैसेज को रोकना ज़्यादा मुश्किल होता है.
शुरू करने से पहले
बहु-स्तरीय पुष्टि (MFA) की सुविधा देने वाली कम से कम एक कंपनी चालू करें. फ़ोन से पुष्टि, पहचान छिपाकर पुष्टि करने, और Apple Game Center के अलावा, हर कंपनी MFA के साथ काम करती है.
उन इलाकों को चालू करें जहां आपको एसएमएस की मदद से पुष्टि करने की सुविधा इस्तेमाल करनी है. Firebase, पूरी तरह से ब्लॉक करने वाले एसएमएस की क्षेत्र नीति का इस्तेमाल करता है. इससे डिफ़ॉल्ट रूप से, आपके प्रोजेक्ट को ज़्यादा सुरक्षित स्थिति में बनाने में मदद मिलती है.
पक्का करें कि आपका ऐप्लिकेशन, उपयोगकर्ता के ईमेल की पुष्टि कर रहा है. MFA के लिए ईमेल पते की पुष्टि करना ज़रूरी है. यह नुकसान पहुंचाने वाले लोगों को किसी ऐसे ईमेल से रजिस्टर करने से रोकता है जिसका मालिकाना हक उनके पास नहीं है. साथ ही, दूसरा तरीका जोड़कर, वे असली मालिक को लॉक कर सकते हैं.
मल्टी-टेनेंसी का इस्तेमाल करना
अगर मल्टी-टेनेंट एनवायरमेंट में इस्तेमाल के लिए, बहु-स्तरीय पुष्टि (MFA) की सुविधा चालू की जा रही है, तो इन चरणों को पूरा करना न भूलें. इस दस्तावेज़ में दिए गए बाकी निर्देशों के अलावा:
GCP कंसोल में जाकर, वह किरायेदार चुनें जिसके साथ आपको काम करना है.
अपने कोड में,
Auth
इंस्टेंस पर मौजूदtenantId
फ़ील्ड को अपने टेनेंट के आईडी पर सेट करें. उदाहरण के लिए:Web
import { getAuth } from "firebase/auth"; const auth = getAuth(app); auth.tenantId = "myTenantId1";
Web
firebase.auth().tenantId = 'myTenantId1';
बहु-स्तरीय पुष्टि (MFA) की सुविधा चालू करना
Firebase कंसोल का पुष्टि करने > साइन इन करने का तरीका पेज खोलें.
बेहतर सेक्शन में, एसएमएस के लिए बहु-स्तरीय पुष्टि (MFA) चालू करें.
आपको वे फ़ोन नंबर भी डालने चाहिए, जिनसे आप अपने ऐप्लिकेशन का परीक्षण करना चाहते हैं. हालांकि, यह ज़रूरी नहीं है, लेकिन डेवलपमेंट के दौरान थ्रॉटलिंग से बचने के लिए टेस्ट फ़ोन नंबरों को रजिस्टर करने का सुझाव दिया जाता है.
अगर आपने पहले से अपने ऐप्लिकेशन के डोमेन को अनुमति नहीं दी है, तो उसे Firebase कंसोल के पुष्टि करने > सेटिंग पेज पर जाकर, अनुमति वाली सूची में जोड़ें.
रजिस्ट्रेशन का पैटर्न चुनना
आपके पास यह चुनने का विकल्प है कि आपके ऐप्लिकेशन के लिए बहु-स्तरीय पुष्टि (MFA) की ज़रूरत है या नहीं. साथ ही, यह भी चुनें कि आपके उपयोगकर्ताओं को कैसे और कब रजिस्टर करना है. कुछ सामान्य पैटर्न में ये शामिल हैं:
रजिस्ट्रेशन के हिस्से के तौर पर, उपयोगकर्ता के दूसरे चरण को रजिस्टर करें. अगर आपके ऐप्लिकेशन को सभी उपयोगकर्ताओं के लिए बहु-स्तरीय पुष्टि (MFA) की ज़रूरत है, तो इस तरीके का इस्तेमाल करें.
रजिस्ट्रेशन के दौरान दूसरा चरण जोड़ने का विकल्प दें, जिसे स्किप किया जा सके. ऐसे ऐप्लिकेशन जो बहु-स्तरीय पुष्टि (MFA) के लिए बढ़ावा देना चाहते हैं, लेकिन ऐसा करना ज़रूरी नहीं है, वे इस तरीके को प्राथमिकता दे सकते हैं.
लोगों को साइन अप स्क्रीन के बजाय, उनके खाते या प्रोफ़ाइल मैनेजमेंट पेज पर जाकर दूसरा चरण जोड़ने की सुविधा दें. इससे रजिस्ट्रेशन की प्रक्रिया के दौरान आने वाली दिक्कतों को कम किया जाता है. साथ ही, सुरक्षा के लिए संवेदनशील उपयोगकर्ताओं के लिए बहु-स्तरीय पुष्टि (MFA) की सुविधा उपलब्ध कराई जाती है.
जब उपयोगकर्ता बढ़ी हुई सुरक्षा ज़रूरतों वाली सुविधाओं को ऐक्सेस करना चाहता है, तो दूसरे चरण को बढ़ाना ज़रूरी होता है.
reCAPTCHA की पुष्टि करने वाला प्रोग्राम सेट अप करना
मैसेज (एसएमएस) कोड भेजने से पहले, आपको reCAPTCHA की पुष्टि करने वाली सुविधा को कॉन्फ़िगर करना होगा. गलत इस्तेमाल रोकने के लिए Firebase reCAPTCHA का इस्तेमाल करता है. इसके लिए यह पक्का किया जाता है कि फ़ोन नंबर की पुष्टि करने के अनुरोध, आपके ऐप्लिकेशन की अनुमति वाले किसी डोमेन से आते हैं.
आपको मैन्युअल तरीके से reCAPTCHA क्लाइंट सेट अप करने की ज़रूरत नहीं है. क्लाइंट SDK टूल का
RecaptchaVerifier
ऑब्जेक्ट, क्लाइंट की सभी ज़रूरी कुंजियां और सीक्रेट अपने-आप बनाता और शुरू करता है.
दिखाई न देने वाले reCAPTCHA का इस्तेमाल करना
RecaptchaVerifier
ऑब्जेक्ट,
न दिखने वाले reCAPTCHA के साथ काम करता है. इसकी मदद से, अक्सर उपयोगकर्ता की पुष्टि की जाती है और इसके लिए किसी अन्य इंटरैक्शन की ज़रूरत नहीं होती. दिखाई न देने वाले reCAPTCHA का इस्तेमाल करने के लिए, invisible
पर size
पैरामीटर सेट करके RecaptchaVerifier
बनाएं. साथ ही, उस यूज़र इंटरफ़ेस (यूआई) एलिमेंट का आईडी बताएं जो मल्टी-फ़ैक्टर रजिस्ट्रेशन शुरू करता है:
Web
import { RecaptchaVerifier } from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier("sign-in-button", {
"size": "invisible",
"callback": function(response) {
// reCAPTCHA solved, you can proceed with
// phoneAuthProvider.verifyPhoneNumber(...).
onSolvedRecaptcha();
}
}, auth);
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 } from "firebase/auth";
const recaptchaVerifier = new 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.
// ...
}
}, auth
);
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 का विजेट आईडी मिलेगा. इसका इस्तेमाल करके, re कैप्चा API को कॉल किया जा सकता है:
var recaptchaResponse = grecaptcha.getResponse(window.recaptchaWidgetId);
reCAPTCHAVerifier इस लॉजिक को पुष्टि करने के तरीके से ऐब्स्ट्रैक्ट कर देता है. इसलिए, आपको grecaptcha
वैरिएबल को सीधे तौर पर हैंडल करने की ज़रूरत नहीं होती.
पुष्टि करने का दूसरा चरण रजिस्टर करना
किसी उपयोगकर्ता के लिए कोई नया सेकंडरी फ़ैक्टर रजिस्टर करने के लिए:
उपयोगकर्ता की फिर से पुष्टि करें.
उपयोगकर्ता से उसका फ़ोन नंबर डालने के लिए कहें.
reCAPTCHA की पुष्टि करने वाली सुविधा को शुरू करें, जैसा कि पिछले सेक्शन में दिखाया गया है. अगर ReCapVerifier इंस्टेंस पहले से कॉन्फ़िगर है, तो यह चरण छोड़ दें:
Web
import { RecaptchaVerifier } from "firebase/auth"; const recaptchaVerifier = new RecaptchaVerifier('recaptcha-container-id', undefined, auth);
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);
MultiFactorAssertion
ऑब्जेक्ट कोPhoneAuthCredential
के साथ शुरू करें: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
} from "firebase/auth";
const recaptchaVerifier = new RecaptchaVerifier('recaptcha-container-id', undefined, auth);
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, 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, एसएएमएल या 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 की पुष्टि करने वाली सुविधा को शुरू करें, जैसा कि पिछले सेक्शन में दिखाया गया है. अगर ReCapVerifier इंस्टेंस पहले से कॉन्फ़िगर है, तो यह चरण छोड़ दें:
Web
import { RecaptchaVerifier } from "firebase/auth"; recaptchaVerifier = new RecaptchaVerifier('recaptcha-container-id', undefined, auth);
Web
var recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container-id');
PhoneInfoOptions
ऑब्जेक्ट को उपयोगकर्ता के फ़ोन नंबर और मल्टी-फ़ैक्टर सेशन के साथ शुरू करें. ये वैल्यू,auth/multi-factor-auth-required
गड़बड़ी को पास किए गएresolver
ऑब्जेक्ट में शामिल हैं: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);
MultiFactorAssertion
ऑब्जेक्ट कोPhoneAuthCredential
के साथ शुरू करें: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('recaptcha-container-id', undefined, auth);
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.
} ...
});
बधाई! आपने बहु-स्तरीय पुष्टि (MFA) की सुविधा का इस्तेमाल करके एक उपयोगकर्ता खाते में साइन इन कर लिया है.
आगे क्या करना है
- एडमिन SDK की मदद से, मल्टी-फ़ैक्टर उपयोगकर्ताओं को मैनेज करें.