@@ -30,7 +30,8 @@ import {
30
30
Auth ,
31
31
OperationType ,
32
32
ProviderId ,
33
- UserCredential
33
+ UserCredential ,
34
+ ConfirmationResult
34
35
// eslint-disable-next-line import/no-extraneous-dependencies
35
36
} from '@firebase/auth-exp' ;
36
37
import { FirebaseError } from '@firebase/util' ;
@@ -39,6 +40,7 @@ import {
39
40
cleanUpTestInstance ,
40
41
getTestInstance
41
42
} from '../../helpers/integration/helpers' ;
43
+ import { getPhoneVerificationCodes } from '../../helpers/integration/emulator_rest_helpers' ;
42
44
43
45
use ( chaiAsPromised ) ;
44
46
@@ -80,9 +82,23 @@ describe('Integration test: phone auth', () => {
80
82
document . body . removeChild ( fakeRecaptchaContainer ) ;
81
83
} ) ;
82
84
85
+ /** If in the emulator, search for the code in the API */
86
+ async function code (
87
+ crOrId : ConfirmationResult | string ,
88
+ fallback : string
89
+ ) : Promise < string > {
90
+ if ( auth . emulatorConfig ) {
91
+ const codes = await getPhoneVerificationCodes ( auth ) ;
92
+ const vid = typeof crOrId === 'string' ? crOrId : crOrId . verificationId ;
93
+ return codes [ vid ] . code ;
94
+ }
95
+
96
+ return fallback ;
97
+ }
98
+
83
99
it ( 'allows user to sign up' , async ( ) => {
84
100
const cr = await signInWithPhoneNumber ( auth , PHONE_A . phoneNumber , verifier ) ;
85
- const userCred = await cr . confirm ( PHONE_A . code ) ;
101
+ const userCred = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
86
102
87
103
expect ( auth . currentUser ) . to . eq ( userCred . user ) ;
88
104
expect ( userCred . operationType ) . to . eq ( OperationType . SIGN_IN ) ;
@@ -98,7 +114,7 @@ describe('Integration test: phone auth', () => {
98
114
const { uid : anonId } = user ;
99
115
100
116
const cr = await linkWithPhoneNumber ( user , PHONE_A . phoneNumber , verifier ) ;
101
- const linkResult = await cr . confirm ( PHONE_A . code ) ;
117
+ const linkResult = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
102
118
expect ( linkResult . operationType ) . to . eq ( OperationType . LINK ) ;
103
119
expect ( linkResult . user . uid ) . to . eq ( user . uid ) ;
104
120
expect ( linkResult . user . phoneNumber ) . to . eq ( PHONE_A . phoneNumber ) ;
@@ -128,7 +144,7 @@ describe('Integration test: phone auth', () => {
128
144
PHONE_A . phoneNumber ,
129
145
verifier
130
146
) ;
131
- signUpCred = await cr . confirm ( PHONE_A . code ) ;
147
+ signUpCred = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
132
148
resetVerifier ( ) ;
133
149
await auth . signOut ( ) ;
134
150
} ) ;
@@ -139,14 +155,14 @@ describe('Integration test: phone auth', () => {
139
155
PHONE_A . phoneNumber ,
140
156
verifier
141
157
) ;
142
- const signInCred = await cr . confirm ( PHONE_A . code ) ;
158
+ const signInCred = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
143
159
144
160
expect ( signInCred . user . uid ) . to . eq ( signUpCred . user . uid ) ;
145
161
} ) ;
146
162
147
163
it ( 'allows the user to update their phone number' , async ( ) => {
148
164
let cr = await signInWithPhoneNumber ( auth , PHONE_A . phoneNumber , verifier ) ;
149
- const { user } = await cr . confirm ( PHONE_A . code ) ;
165
+ const { user } = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
150
166
151
167
resetVerifier ( ) ;
152
168
@@ -158,38 +174,48 @@ describe('Integration test: phone auth', () => {
158
174
159
175
await updatePhoneNumber (
160
176
user ,
161
- PhoneAuthProvider . credential ( verificationId , PHONE_B . code )
177
+ PhoneAuthProvider . credential (
178
+ verificationId ,
179
+ await code ( verificationId , PHONE_B . code )
180
+ )
162
181
) ;
163
182
expect ( user . phoneNumber ) . to . eq ( PHONE_B . phoneNumber ) ;
164
183
165
184
await auth . signOut ( ) ;
166
185
resetVerifier ( ) ;
167
186
168
187
cr = await signInWithPhoneNumber ( auth , PHONE_B . phoneNumber , verifier ) ;
169
- const { user : secondSignIn } = await cr . confirm ( PHONE_B . code ) ;
188
+ const { user : secondSignIn } = await cr . confirm (
189
+ await code ( cr , PHONE_B . code )
190
+ ) ;
170
191
expect ( secondSignIn . uid ) . to . eq ( user . uid ) ;
171
192
} ) ;
172
193
173
194
it ( 'allows the user to reauthenticate with phone number' , async ( ) => {
174
195
let cr = await signInWithPhoneNumber ( auth , PHONE_A . phoneNumber , verifier ) ;
175
- const { user } = await cr . confirm ( PHONE_A . code ) ;
196
+ const { user } = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
176
197
const oldToken = await user . getIdToken ( ) ;
177
198
178
199
resetVerifier ( ) ;
179
200
201
+ // Wait a bit to ensure the sign in time is different in the token
202
+ await new Promise ( ( resolve ) : void => {
203
+ setTimeout ( resolve , 1500 ) ;
204
+ } ) ;
205
+
180
206
cr = await reauthenticateWithPhoneNumber (
181
207
user ,
182
208
PHONE_A . phoneNumber ,
183
209
verifier
184
210
) ;
185
- await cr . confirm ( PHONE_A . code ) ;
211
+ await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
186
212
187
213
expect ( await user . getIdToken ( ) ) . not . to . eq ( oldToken ) ;
188
214
} ) ;
189
215
190
216
it ( 'prevents reauthentication with wrong phone number' , async ( ) => {
191
217
let cr = await signInWithPhoneNumber ( auth , PHONE_A . phoneNumber , verifier ) ;
192
- const { user } = await cr . confirm ( PHONE_A . code ) ;
218
+ const { user } = await cr . confirm ( await code ( cr , PHONE_A . code ) ) ;
193
219
194
220
resetVerifier ( ) ;
195
221
@@ -198,7 +224,7 @@ describe('Integration test: phone auth', () => {
198
224
PHONE_B . phoneNumber ,
199
225
verifier
200
226
) ;
201
- await expect ( cr . confirm ( PHONE_B . code ) ) . to . be . rejectedWith (
227
+ await expect ( cr . confirm ( await code ( cr , PHONE_B . code ) ) ) . to . be . rejectedWith (
202
228
FirebaseError ,
203
229
'auth/user-mismatch'
204
230
) ;
@@ -207,7 +233,9 @@ describe('Integration test: phone auth', () => {
207
233
// reauthenticateWithPhoneNumber does not trigger a state change
208
234
resetVerifier ( ) ;
209
235
cr = await signInWithPhoneNumber ( auth , PHONE_B . phoneNumber , verifier ) ;
210
- const { user : otherUser } = await cr . confirm ( PHONE_B . code ) ;
236
+ const { user : otherUser } = await cr . confirm (
237
+ await code ( cr , PHONE_B . code )
238
+ ) ;
211
239
await otherUser . delete ( ) ;
212
240
} ) ;
213
241
} ) ;
0 commit comments