UNPKG

6.68 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17import { Auth, PhoneInfoOptions, ApplicationVerifier, UserCredential } from '../../model/public_types';
18import { PhoneAuthCredential } from '../../core/credentials/phone';
19import { AuthCredential } from '../../core';
20import { FirebaseError } from '@firebase/util';
21/**
22 * Provider for generating an {@link PhoneAuthCredential}.
23 *
24 * @example
25 * ```javascript
26 * // 'recaptcha-container' is the ID of an element in the DOM.
27 * const applicationVerifier = new RecaptchaVerifier('recaptcha-container');
28 * const provider = new PhoneAuthProvider(auth);
29 * const verificationId = await provider.verifyPhoneNumber('+16505550101', applicationVerifier);
30 * // Obtain the verificationCode from the user.
31 * const phoneCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
32 * const userCredential = await signInWithCredential(auth, phoneCredential);
33 * ```
34 *
35 * @public
36 */
37export declare class PhoneAuthProvider {
38 /** Always set to {@link ProviderId}.PHONE. */
39 static readonly PROVIDER_ID: 'phone';
40 /** Always set to {@link SignInMethod}.PHONE. */
41 static readonly PHONE_SIGN_IN_METHOD: 'phone';
42 /** Always set to {@link ProviderId}.PHONE. */
43 readonly providerId: "phone";
44 private readonly auth;
45 /**
46 * @param auth - The Firebase {@link Auth} instance in which sign-ins should occur.
47 *
48 */
49 constructor(auth: Auth);
50 /**
51 *
52 * Starts a phone number authentication flow by sending a verification code to the given phone
53 * number.
54 *
55 * @example
56 * ```javascript
57 * const provider = new PhoneAuthProvider(auth);
58 * const verificationId = await provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
59 * // Obtain verificationCode from the user.
60 * const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
61 * const userCredential = await signInWithCredential(auth, authCredential);
62 * ```
63 *
64 * @example
65 * An alternative flow is provided using the `signInWithPhoneNumber` method.
66 * ```javascript
67 * const confirmationResult = signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
68 * // Obtain verificationCode from the user.
69 * const userCredential = confirmationResult.confirm(verificationCode);
70 * ```
71 *
72 * @param phoneInfoOptions - The user's {@link PhoneInfoOptions}. The phone number should be in
73 * E.164 format (e.g. +16505550101).
74 * @param applicationVerifier - For abuse prevention, this method also requires a
75 * {@link ApplicationVerifier}. This SDK includes a reCAPTCHA-based implementation,
76 * {@link RecaptchaVerifier}.
77 *
78 * @returns A Promise for a verification ID that can be passed to
79 * {@link PhoneAuthProvider.credential} to identify this flow..
80 */
81 verifyPhoneNumber(phoneOptions: PhoneInfoOptions | string, applicationVerifier: ApplicationVerifier): Promise<string>;
82 /**
83 * Creates a phone auth credential, given the verification ID from
84 * {@link PhoneAuthProvider.verifyPhoneNumber} and the code that was sent to the user's
85 * mobile device.
86 *
87 * @example
88 * ```javascript
89 * const provider = new PhoneAuthProvider(auth);
90 * const verificationId = provider.verifyPhoneNumber(phoneNumber, applicationVerifier);
91 * // Obtain verificationCode from the user.
92 * const authCredential = PhoneAuthProvider.credential(verificationId, verificationCode);
93 * const userCredential = signInWithCredential(auth, authCredential);
94 * ```
95 *
96 * @example
97 * An alternative flow is provided using the `signInWithPhoneNumber` method.
98 * ```javascript
99 * const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, applicationVerifier);
100 * // Obtain verificationCode from the user.
101 * const userCredential = await confirmationResult.confirm(verificationCode);
102 * ```
103 *
104 * @param verificationId - The verification ID returned from {@link PhoneAuthProvider.verifyPhoneNumber}.
105 * @param verificationCode - The verification code sent to the user's mobile device.
106 *
107 * @returns The auth provider credential.
108 */
109 static credential(verificationId: string, verificationCode: string): PhoneAuthCredential;
110 /**
111 * Generates an {@link AuthCredential} from a {@link UserCredential}.
112 * @param userCredential - The user credential.
113 */
114 static credentialFromResult(userCredential: UserCredential): AuthCredential | null;
115 /**
116 * Returns an {@link AuthCredential} when passed an error.
117 *
118 * @remarks
119 *
120 * This method works for errors like
121 * `auth/account-exists-with-different-credentials`. This is useful for
122 * recovering when attempting to set a user's phone number but the number
123 * in question is already tied to another account. For example, the following
124 * code tries to update the current user's phone number, and if that
125 * fails, links the user with the account associated with that number:
126 *
127 * ```js
128 * const provider = new PhoneAuthProvider(auth);
129 * const verificationId = await provider.verifyPhoneNumber(number, verifier);
130 * try {
131 * const code = ''; // Prompt the user for the verification code
132 * await updatePhoneNumber(
133 * auth.currentUser,
134 * PhoneAuthProvider.credential(verificationId, code));
135 * } catch (e) {
136 * if (e.code === 'auth/account-exists-with-different-credential') {
137 * const cred = PhoneAuthProvider.credentialFromError(e);
138 * await linkWithCredential(auth.currentUser, cred);
139 * }
140 * }
141 *
142 * // At this point, auth.currentUser.phoneNumber === number.
143 * ```
144 *
145 * @param error - The error to generate a credential from.
146 */
147 static credentialFromError(error: FirebaseError): AuthCredential | null;
148 private static credentialFromTaggedObject;
149}