@@ -21,16 +21,17 @@ import {
21
21
sendSignInLinkToEmail as _sendSignInLinkToEmail ,
22
22
signInAnonymously as _signInAnonymously ,
23
23
signInWithPhoneNumber as _signInWithPhoneNumber ,
24
+ signInWithCredential as _signInWithCredential ,
24
25
ActionCodeSettings ,
25
26
ApplicationVerifier ,
26
27
AuthProvider ,
27
28
ConfirmationResult ,
28
29
EmailAuthProvider ,
29
30
linkWithCredential ,
30
31
PhoneAuthProvider ,
31
- signInWithCredential ,
32
32
signInWithRedirect ,
33
33
UserCredential ,
34
+ AuthCredential ,
34
35
} from "firebase/auth" ;
35
36
import { FirebaseUIConfiguration } from "./config" ;
36
37
import { handleFirebaseError } from "./errors" ;
@@ -70,7 +71,7 @@ export async function signInWithEmailAndPassword(
70
71
}
71
72
72
73
ui . setState ( "pending" ) ;
73
- const result = await signInWithCredential ( ui . auth , credential ) ;
74
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
74
75
return handlePendingCredential ( ui , result ) ;
75
76
} catch ( error ) {
76
77
handleFirebaseError ( ui , error ) ;
@@ -138,7 +139,7 @@ export async function confirmPhoneNumber(
138
139
}
139
140
140
141
ui . setState ( "pending" ) ;
141
- const result = await signInWithCredential ( ui . auth , credential ) ;
142
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
142
143
return handlePendingCredential ( ui , result ) ;
143
144
} catch ( error ) {
144
145
handleFirebaseError ( ui , error ) ;
@@ -182,18 +183,27 @@ export async function signInWithEmailLink(
182
183
email : string ,
183
184
link : string
184
185
) : Promise < UserCredential > {
185
- try {
186
- const credential = EmailAuthProvider . credentialWithLink ( email , link ) ;
186
+ const credential = EmailAuthProvider . credentialWithLink ( email , link ) ;
187
+ return signInWithCredential ( ui , credential ) ;
188
+ }
187
189
190
+ export async function signInWithCredential (
191
+ ui : FirebaseUIConfiguration ,
192
+ credential : AuthCredential
193
+ ) : Promise < UserCredential > {
194
+ try {
188
195
if ( hasBehavior ( ui , "autoUpgradeAnonymousCredential" ) ) {
189
- const result = await getBehavior ( ui , "autoUpgradeAnonymousCredential" ) ( ui , credential ) ;
190
- if ( result ) {
191
- return handlePendingCredential ( ui , result ) ;
196
+ const userCredential = await getBehavior ( ui , "autoUpgradeAnonymousCredential" ) ( ui , credential ) ;
197
+
198
+ // If they got here, they're either not anonymous or they've been linked.
199
+ // If the credential has been linked, we don't need to sign them in, so return early.
200
+ if ( userCredential ) {
201
+ return handlePendingCredential ( ui , userCredential ) ;
192
202
}
193
203
}
194
204
195
205
ui . setState ( "pending" ) ;
196
- const result = await signInWithCredential ( ui . auth , credential ) ;
206
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
197
207
return handlePendingCredential ( ui , result ) ;
198
208
} catch ( error ) {
199
209
handleFirebaseError ( ui , error ) ;
0 commit comments