ผู้ให้บริการสำหรับการสร้าง PhoneAuthCredential .
PhoneAuthProvider
ไม่ทำงานในสภาพแวดล้อม Node.js
ลายเซ็น:
export declare class PhoneAuthProvider
คอนสตรัคเตอร์
ตัวสร้าง | ตัวดัดแปลง | คำอธิบาย |
---|---|---|
(ตัวสร้าง) (รับรองความถูกต้อง) | สร้างอินสแตนซ์ใหม่ของคลาส PhoneAuthProvider |
คุณสมบัติ
คุณสมบัติ | ตัวดัดแปลง | พิมพ์ | คำอธิบาย |
---|---|---|---|
PHONE_SIGN_IN_METHOD | static | 'โทรศัพท์' | ตั้งค่าเป็น SignInMethod เสมอ .โทรศัพท์. |
PROVIDER_ID | static | 'โทรศัพท์' | ตั้งค่าเป็น ProviderId เสมอ .โทรศัพท์. |
รหัสผู้ให้บริการ | "โทรศัพท์" | ตั้งค่าเป็น ProviderId เสมอ .โทรศัพท์. |
วิธีการ
วิธี | ตัวดัดแปลง | คำอธิบาย |
---|---|---|
ข้อมูลประจำตัว (รหัสยืนยัน, รหัสยืนยัน) | static | สร้างข้อมูลรับรองการตรวจสอบสิทธิ์ทางโทรศัพท์ โดยระบุรหัสยืนยันจาก PhoneAuthProvider.verifyPhoneNumber() และรหัสที่ส่งไปยังอุปกรณ์เคลื่อนที่ของผู้ใช้ |
ข้อมูลรับรองจากข้อผิดพลาด (ข้อผิดพลาด) | static | ส่งกลับ AuthCredential เมื่อผ่านข้อผิดพลาด |
หนังสือรับรองจากผลลัพธ์ (ข้อมูลรับรองผู้ใช้) | static | สร้าง AuthCredential จาก UserCredential . |
ตรวจสอบหมายเลขโทรศัพท์ (phoneOptions, applicationVerifier) | เริ่มขั้นตอนการตรวจสอบสิทธิ์หมายเลขโทรศัพท์โดยการส่งรหัสยืนยันไปยังหมายเลขโทรศัพท์ที่ระบุ |
PhoneAuthProvider (ตัวสร้าง)
สร้างอินสแตนซ์ใหม่ของคลาส PhoneAuthProvider
ลายเซ็น:
constructor(auth: Auth);
พารามิเตอร์
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
รับรองความถูกต้อง | การรับรองความถูกต้อง | อินสแตนซ์ Firebase Auth ที่ควรลงชื่อเข้าใช้ |
PhoneAuthProvider.PHONE_SIGN_IN_METHOD
ตั้งค่าเป็น SignInMethod เสมอ .โทรศัพท์.
ลายเซ็น:
static readonly PHONE_SIGN_IN_METHOD: 'phone';
PhoneAuthProvider.PROVIDER_ID
ตั้งค่าเป็น ProviderId เสมอ .โทรศัพท์.
ลายเซ็น:
static readonly PROVIDER_ID: 'phone';
PhoneAuthProvider.providerId
ตั้งค่าเป็น ProviderId เสมอ .โทรศัพท์.
ลายเซ็น:
readonly providerId: "phone";
PhoneAuthProvider.credential()
สร้างข้อมูลรับรองการตรวจสอบสิทธิ์ทางโทรศัพท์ โดยระบุรหัสยืนยันจาก PhoneAuthProvider.verifyPhoneNumber() และรหัสที่ส่งไปยังอุปกรณ์เคลื่อนที่ของผู้ใช้
ลายเซ็น:
static credential(verificationId: string, verificationCode: string): PhoneAuthCredential;
พารามิเตอร์
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
รหัสยืนยัน | เชือก | รหัสยืนยันที่ส่งคืนจาก PhoneAuthProvider.verifyPhoneNumber() . |
รหัสยืนยัน | เชือก | รหัสยืนยันที่ส่งไปยังอุปกรณ์มือถือของผู้ใช้ |
ผลตอบแทน:
ข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์
ตัวอย่างที่ 1
const provider = new PhoneAuthProvider(auth);
const verificationId = provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = signInWithCredential(auth, authCredential);
ตัวอย่างที่ 2
ขั้นตอนทางเลือกมีให้โดยใช้เมธอด signInWithPhoneNumber
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = await confirmationResult.confirm(verificationCode);
PhoneAuthProvider.credentialFromError()
ส่งกลับ AuthCredential เมื่อผ่านข้อผิดพลาด
วิธีการนี้ใช้ได้กับข้อผิดพลาด เช่น auth/account-exists-with-different-credentials
. สิ่งนี้มีประโยชน์สำหรับการกู้คืนเมื่อพยายามตั้งค่าหมายเลขโทรศัพท์ของผู้ใช้ แต่หมายเลขดังกล่าวเชื่อมโยงกับบัญชีอื่นแล้ว ตัวอย่างเช่น รหัสต่อไปนี้พยายามอัปเดตหมายเลขโทรศัพท์ของผู้ใช้ปัจจุบัน และหากล้มเหลว ให้เชื่อมโยงผู้ใช้กับบัญชีที่เชื่อมโยงกับหมายเลขนั้น:
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(number, verifier);
try {
const code = ''; // Prompt the user for the verification code
await updatePhoneNumber(
auth.currentUser,
PhoneAuthProvider.credential(verificationId, code));
} catch (e) {
if ((e as FirebaseError)?.code === 'auth/account-exists-with-different-credential') {
const cred = PhoneAuthProvider.credentialFromError(e);
await linkWithCredential(auth.currentUser, cred);
}
}
// At this point, auth.currentUser.phoneNumber === number.
ลายเซ็น:
static credentialFromError(error: FirebaseError): AuthCredential | null;
พารามิเตอร์
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
ข้อผิดพลาด | ข้อผิดพลาดของ Firebase | เกิดข้อผิดพลาดในการสร้างข้อมูลรับรองจาก |
ผลตอบแทน:
ข้อมูลรับรองความถูกต้อง | โมฆะ
PhoneAuthProvider.credentialFromResult()
สร้าง AuthCredential จาก UserCredential .
ลายเซ็น:
static credentialFromResult(userCredential: UserCredential): AuthCredential | null;
พารามิเตอร์
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
ข้อมูลรับรองผู้ใช้ | ข้อมูลรับรองผู้ใช้ | ข้อมูลรับรองผู้ใช้ |
ผลตอบแทน:
ข้อมูลรับรองความถูกต้อง | โมฆะ
PhoneAuthProvider.verifyPhoneNumber()
เริ่มขั้นตอนการตรวจสอบสิทธิ์หมายเลขโทรศัพท์โดยการส่งรหัสยืนยันไปยังหมายเลขโทรศัพท์ที่ระบุ
ลายเซ็น:
verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;
พารามิเตอร์
พารามิเตอร์ | พิมพ์ | คำอธิบาย |
---|---|---|
ตัวเลือกโทรศัพท์ | ข้อมูลโทรศัพท์ตัวเลือก | เชือก | |
แอปพลิเคชันVerifier | ApplicationVerifier | สำหรับการป้องกันการละเมิด วิธีการนี้ต้องใช้ ApplicationVerifier ด้วย . SDK นี้มีการใช้งาน RecaptchaVerifier ที่ใช้ reCAPTCHA . |
ผลตอบแทน:
สัญญา<สตริง>
คำมั่นสัญญาสำหรับรหัสยืนยันที่สามารถส่งผ่านไปยัง PhoneAuthProvider.credential() เพื่อระบุโฟลว์นี้
ตัวอย่างที่ 1
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, authCredential);
ตัวอย่างที่ 2
ขั้นตอนทางเลือกมีให้โดยใช้เมธอด signInWithPhoneNumber
const confirmationResult = signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
// Obtain verificationCode from the user.
const userCredential = confirmationResult.confirm(verificationCode);
ตัวอย่าง
// 'recaptcha-container' is the ID of an element in the DOM.
const applicationVerifier = new RecaptchaVerifier('recaptcha-container');
const provider = new PhoneAuthProvider(auth);
const verificationId = await provider.verifyPhoneNumber('+16505550101', applicationVerifier);
// Obtain the verificationCode from the user.
const phoneCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
const userCredential = await signInWithCredential(auth, phoneCredential);