@@ -21,6 +21,21 @@ import {
21
21
import { handleFirebaseError } from './errors' ;
22
22
import { type TranslationsConfig } from './translations' ;
23
23
24
+ async function handlePendingCredential ( user : UserCredential ) : Promise < UserCredential > {
25
+ const pendingCredString = window . sessionStorage . getItem ( 'pendingCred' ) ;
26
+ if ( ! pendingCredString ) return user ;
27
+
28
+ try {
29
+ const pendingCred = JSON . parse ( pendingCredString ) ;
30
+ const result = await linkWithCredential ( user . user , pendingCred ) ;
31
+ window . sessionStorage . removeItem ( 'pendingCred' ) ;
32
+ return result ;
33
+ } catch ( error ) {
34
+ window . sessionStorage . removeItem ( 'pendingCred' ) ;
35
+ return user ;
36
+ }
37
+ }
38
+
24
39
export async function fuiSignInWithEmailAndPassword (
25
40
auth : Auth ,
26
41
email : string ,
@@ -29,19 +44,22 @@ export async function fuiSignInWithEmailAndPassword(
29
44
language ?: string ;
30
45
translations ?: TranslationsConfig ;
31
46
enableAutoUpgradeAnonymous ?: boolean ;
47
+ enableHandleExistingCredential ?: boolean ;
32
48
}
33
49
) : Promise < UserCredential > {
34
50
try {
35
51
const currentUser = auth . currentUser ;
36
52
const credential = EmailAuthProvider . credential ( email , password ) ;
37
53
38
54
if ( currentUser ?. isAnonymous && opts ?. enableAutoUpgradeAnonymous ) {
39
- return await linkWithCredential ( currentUser , credential ) ;
55
+ const result = await linkWithCredential ( currentUser , credential ) ;
56
+ return handlePendingCredential ( result ) ;
40
57
}
41
58
42
- return await signInWithCredential ( auth , credential ) ;
59
+ const result = await signInWithCredential ( auth , credential ) ;
60
+ return handlePendingCredential ( result ) ;
43
61
} catch ( error ) {
44
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
62
+ return handleFirebaseError ( error , opts ) ;
45
63
}
46
64
}
47
65
@@ -53,19 +71,22 @@ export async function fuiCreateUserWithEmailAndPassword(
53
71
language ?: string ;
54
72
translations ?: TranslationsConfig ;
55
73
enableAutoUpgradeAnonymous ?: boolean ;
74
+ enableHandleExistingCredential ?: boolean ;
56
75
}
57
76
) : Promise < UserCredential > {
58
77
try {
59
78
const currentUser = auth . currentUser ;
60
79
const credential = EmailAuthProvider . credential ( email , password ) ;
61
80
62
81
if ( currentUser ?. isAnonymous && opts ?. enableAutoUpgradeAnonymous ) {
63
- return await linkWithCredential ( currentUser , credential ) ;
82
+ const result = await linkWithCredential ( currentUser , credential ) ;
83
+ return handlePendingCredential ( result ) ;
64
84
}
65
85
66
- return await createUserWithEmailAndPassword ( auth , email , password ) ;
86
+ const result = await createUserWithEmailAndPassword ( auth , email , password ) ;
87
+ return handlePendingCredential ( result ) ;
67
88
} catch ( error ) {
68
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
89
+ return handleFirebaseError ( error , opts ) ;
69
90
}
70
91
}
71
92
@@ -81,7 +102,7 @@ export async function fuiSignInWithPhoneNumber(
81
102
try {
82
103
return await signInWithPhoneNumber ( auth , phoneNumber , recaptchaVerifier ) ;
83
104
} catch ( error ) {
84
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
105
+ return ( await handleFirebaseError ( error , opts ) ) as never ;
85
106
}
86
107
}
87
108
@@ -92,6 +113,7 @@ export async function fuiConfirmPhoneNumber(
92
113
language ?: string ;
93
114
translations ?: TranslationsConfig ;
94
115
enableAutoUpgradeAnonymous ?: boolean ;
116
+ enableHandleExistingCredential ?: boolean ;
95
117
}
96
118
) : Promise < UserCredential > {
97
119
try {
@@ -101,12 +123,13 @@ export async function fuiConfirmPhoneNumber(
101
123
102
124
if ( currentUser ?. isAnonymous && opts ?. enableAutoUpgradeAnonymous ) {
103
125
const result = await linkWithCredential ( currentUser , credential ) ;
104
- return result ;
126
+ return handlePendingCredential ( result ) ;
105
127
}
106
128
107
- return await signInWithCredential ( auth , credential ) ;
129
+ const result = await signInWithCredential ( auth , credential ) ;
130
+ return handlePendingCredential ( result ) ;
108
131
} catch ( error ) {
109
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
132
+ return handleFirebaseError ( error , opts ) ;
110
133
}
111
134
}
112
135
@@ -121,7 +144,7 @@ export async function fuiSendPasswordResetEmail(
121
144
try {
122
145
await sendPasswordResetEmail ( auth , email ) ;
123
146
} catch ( error ) {
124
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
147
+ return ( await handleFirebaseError ( error , opts ) ) as never ;
125
148
}
126
149
}
127
150
@@ -148,7 +171,7 @@ export async function fuiSendSignInLinkToEmail(
148
171
await sendSignInLinkToEmail ( auth , email , actionCodeSettings ) ;
149
172
window . localStorage . setItem ( 'emailForSignIn' , email ) ;
150
173
} catch ( error ) {
151
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
174
+ return ( await handleFirebaseError ( error , opts ) ) as never ;
152
175
}
153
176
}
154
177
@@ -164,6 +187,7 @@ export async function fuiSignInWithEmailLink(
164
187
language ?: string ;
165
188
translations ?: TranslationsConfig ;
166
189
enableAutoUpgradeAnonymous ?: boolean ;
190
+ enableHandleExistingCredential ?: boolean ;
167
191
}
168
192
) : Promise < UserCredential > {
169
193
try {
@@ -174,14 +198,14 @@ export async function fuiSignInWithEmailLink(
174
198
if ( currentUser ?. isAnonymous && isAnonymousUpgrade && opts ?. enableAutoUpgradeAnonymous ) {
175
199
const result = await linkWithCredential ( currentUser , credential ) ;
176
200
window . localStorage . removeItem ( 'emailLinkAnonymousUpgrade' ) ;
177
- return result ;
201
+ return handlePendingCredential ( result ) ;
178
202
}
179
203
180
204
const result = await signInWithCredential ( auth , credential ) ;
181
205
window . localStorage . removeItem ( 'emailLinkAnonymousUpgrade' ) ;
182
- return result ;
206
+ return handlePendingCredential ( result ) ;
183
207
} catch ( error ) {
184
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
208
+ return handleFirebaseError ( error , opts ) ;
185
209
}
186
210
}
187
211
@@ -193,9 +217,10 @@ export async function fuiSignInAnonymously(
193
217
}
194
218
) : Promise < UserCredential > {
195
219
try {
196
- return await signInAnonymously ( auth ) ;
220
+ const result = await signInAnonymously ( auth ) ;
221
+ return handlePendingCredential ( result ) ;
197
222
} catch ( error ) {
198
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
223
+ return handleFirebaseError ( error , opts ) ;
199
224
}
200
225
}
201
226
@@ -206,6 +231,7 @@ export async function fuiSignInWithOAuth(
206
231
language ?: string ;
207
232
translations ?: TranslationsConfig ;
208
233
enableAutoUpgradeAnonymous ?: boolean ;
234
+ enableHandleExistingCredential ?: boolean ;
209
235
}
210
236
) : Promise < void > {
211
237
try {
@@ -217,7 +243,7 @@ export async function fuiSignInWithOAuth(
217
243
await signInWithRedirect ( auth , provider ) ;
218
244
}
219
245
} catch ( error ) {
220
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
246
+ return ( await handleFirebaseError ( error , opts ) ) as never ;
221
247
}
222
248
}
223
249
@@ -228,6 +254,7 @@ export async function fuiCompleteEmailLinkSignIn(
228
254
language ?: string ;
229
255
translations ?: TranslationsConfig ;
230
256
enableAutoUpgradeAnonymous ?: boolean ;
257
+ enableHandleExistingCredential ?: boolean ;
231
258
}
232
259
) : Promise < UserCredential | null > {
233
260
try {
@@ -239,9 +266,11 @@ export async function fuiCompleteEmailLinkSignIn(
239
266
if ( ! email ) return null ;
240
267
241
268
const result = await fuiSignInWithEmailLink ( auth , email , currentUrl , opts ) ;
242
- window . localStorage . removeItem ( 'emailForSignIn' ) ;
243
- return result ;
269
+ return handlePendingCredential ( result ) ;
244
270
} catch ( error ) {
245
- handleFirebaseError ( error , opts ?. translations , opts ?. language ) ;
271
+ return handleFirebaseError ( error , opts ) ;
272
+ } finally {
273
+ window . localStorage . removeItem ( 'emailForSignIn' ) ;
274
+ window . localStorage . removeItem ( 'emailLinkAnonymousUpgrade' ) ;
246
275
}
247
276
}
0 commit comments