@@ -21,15 +21,16 @@ 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
UserCredential ,
33
+ AuthCredential ,
33
34
} from "firebase/auth" ;
34
35
import { FirebaseUIConfiguration } from "./config" ;
35
36
import { handleFirebaseError } from "./errors" ;
@@ -69,7 +70,7 @@ export async function signInWithEmailAndPassword(
69
70
}
70
71
71
72
ui . setState ( "pending" ) ;
72
- const result = await signInWithCredential ( ui . auth , credential ) ;
73
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
73
74
return handlePendingCredential ( ui , result ) ;
74
75
} catch ( error ) {
75
76
handleFirebaseError ( ui , error ) ;
@@ -137,7 +138,7 @@ export async function confirmPhoneNumber(
137
138
}
138
139
139
140
ui . setState ( "pending" ) ;
140
- const result = await signInWithCredential ( ui . auth , credential ) ;
141
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
141
142
return handlePendingCredential ( ui , result ) ;
142
143
} catch ( error ) {
143
144
handleFirebaseError ( ui , error ) ;
@@ -181,18 +182,27 @@ export async function signInWithEmailLink(
181
182
email : string ,
182
183
link : string
183
184
) : Promise < UserCredential > {
184
- try {
185
- const credential = EmailAuthProvider . credentialWithLink ( email , link ) ;
185
+ const credential = EmailAuthProvider . credentialWithLink ( email , link ) ;
186
+ return signInWithCredential ( ui , credential ) ;
187
+ }
186
188
189
+ export async function signInWithCredential (
190
+ ui : FirebaseUIConfiguration ,
191
+ credential : AuthCredential
192
+ ) : Promise < UserCredential > {
193
+ try {
187
194
if ( hasBehavior ( ui , "autoUpgradeAnonymousCredential" ) ) {
188
- const result = await getBehavior ( ui , "autoUpgradeAnonymousCredential" ) ( ui , credential ) ;
189
- if ( result ) {
190
- return handlePendingCredential ( ui , result ) ;
195
+ const userCredential = await getBehavior ( ui , "autoUpgradeAnonymousCredential" ) ( ui , credential ) ;
196
+
197
+ // If they got here, they're either not anonymous or they've been linked.
198
+ // If the credential has been linked, we don't need to sign them in, so return early.
199
+ if ( userCredential ) {
200
+ return handlePendingCredential ( ui , userCredential ) ;
191
201
}
192
202
}
193
203
194
204
ui . setState ( "pending" ) ;
195
- const result = await signInWithCredential ( ui . auth , credential ) ;
205
+ const result = await _signInWithCredential ( ui . auth , credential ) ;
196
206
return handlePendingCredential ( ui , result ) ;
197
207
} catch ( error ) {
198
208
handleFirebaseError ( ui , error ) ;
0 commit comments