用于生成PhoneAuthCredential 的提供程序。
PhoneAuthProvider
不适用于 Node.js 环境。
签名:
export declare class PhoneAuthProvider
构造函数
构造函数 | 修饰符 | 描述 |
---|---|---|
(构造函数)(授权) | 构造PhoneAuthProvider 类的新实例 |
特性
财产 | 修饰符 | 类型 | 描述 |
---|---|---|---|
PHONE_SIGN_IN_METHOD | static | '电话' | 始终设置为SignInMethod 。电话。 |
提供商_ID | static | '电话' | 始终设置为ProviderId 。电话。 |
提供者ID | “电话” | 始终设置为ProviderId 。电话。 |
方法
方法 | 修饰符 | 描述 |
---|---|---|
凭证(验证ID,验证码) | static | 根据来自PhoneAuthProvider.verifyPhoneNumber()的验证 ID 和发送到用户移动设备的代码,创建电话身份验证凭据。 |
凭证来自错误(错误) | static | 传递错误时返回AuthCredential 。 |
凭证来自结果(用户凭证) | static | 从UserCredential生成AuthCredential 。 |
verifyPhoneNumber(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()的验证 ID 和发送到用户移动设备的代码,创建电话身份验证凭据。
签名:
static credential(verificationId: string, verificationCode: string): PhoneAuthCredential;
参数
范围 | 类型 | 描述 |
---|---|---|
验证ID | 细绳 | 从PhoneAuthProvider.verifyPhoneNumber()返回的验证 ID 。 |
验证码 | 细绳 | 发送到用户移动设备的验证码。 |
返回:
身份验证提供者凭证。
实施例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()
从UserCredential生成AuthCredential 。
签名:
static credentialFromResult(userCredential: UserCredential): AuthCredential | null;
参数
范围 | 类型 | 描述 |
---|---|---|
用户凭证 | 用户凭证 | 用户凭证。 |
返回:
验证凭证|无效的
PhoneAuthProvider.verifyPhoneNumber()
通过向给定电话号码发送验证码来启动电话号码身份验证流程。
签名:
verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;
参数
范围 | 类型 | 描述 |
---|---|---|
电话选项 | 电话信息选项|细绳 | |
应用程序验证器 | 应用验证器 | 为了防止滥用,此方法还需要一个ApplicationVerifier 。该 SDK 包括基于 reCAPTCHA 的实现RecaptchaVerifier 。 |
返回:
承诺<字符串>
验证 ID 的 Promise,可以传递给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);