@@ -911,7 +911,7 @@ firebaseui.auth.AuthUI.prototype.startSignInWithEmailAndPassword =
911
911
* Create a new email and password account.
912
912
* @param {string } email The email to sign up with.
913
913
* @param {string } password The password to sign up with.
914
- * @return {!firebase.Promise<!firebase.User > }
914
+ * @return {!firebase.Promise<!firebase.auth.UserCredential > }
915
915
*/
916
916
firebaseui . auth . AuthUI . prototype . startCreateUserWithEmailAndPassword =
917
917
function ( email , password ) {
@@ -925,17 +925,15 @@ firebaseui.auth.AuthUI.prototype.startCreateUserWithEmailAndPassword =
925
925
// For anonymous user upgrade, call link with credential on the external
926
926
// Auth user. Otherwise merge conflict will always occur when linking the
927
927
// credential to the external anonymous user after creation.
928
- return /** @type {!firebase.Promise<!firebase.User> } */ (
929
- user . linkAndRetrieveDataWithCredential ( credential )
930
- . then ( function ( result ) {
931
- return result [ 'user' ] ;
932
- } ) ) ;
928
+ return /** @type {!firebase.Promise<!firebase.auth.UserCredential> } */ (
929
+ user . linkAndRetrieveDataWithCredential ( credential ) ) ;
933
930
} else {
934
931
// Start create user with email and password. This runs on the internal
935
932
// Auth instance as finish sign in will sign in with that same credential
936
933
// to developer Auth instance.
937
- return /** @type {!firebase.Promise<!firebase.User> } */ (
938
- self . getAuth ( ) . createUserWithEmailAndPassword ( email , password ) ) ;
934
+ return /** @type {!firebase.Promise<!firebase.auth.UserCredential> } */ (
935
+ self . getAuth ( ) . createUserAndRetrieveDataWithEmailAndPassword (
936
+ email , password ) ) ;
939
937
}
940
938
} ;
941
939
// Initialize current user if auto upgrade is enabled beforing running
@@ -1149,24 +1147,28 @@ firebaseui.auth.AuthUI.prototype.finishSignInWithCredential =
1149
1147
this . checkIfDestroyed_ ( ) ;
1150
1148
var self = this ;
1151
1149
var cb = function ( user ) {
1152
- // Anonymous user upgrade successful, resolve immediately with the user.
1153
- // No need to sign in again with the same credential on the external Auth
1154
- // instance.
1150
+ // Anonymous user upgrade successful, sign out on internal instance and
1151
+ // resolve with the user. No need to sign in again with the same credential
1152
+ // on the external Auth instance.
1155
1153
// If user is signed in on internal instance, ignore the user on external
1156
- // instance and finish the sign in on external instance.
1154
+ // instance, sign out on internal instance and finish the sign in on
1155
+ // external instance.
1157
1156
if ( self . currentUser_ &&
1158
1157
! self . currentUser_ [ 'isAnonymous' ] &&
1159
1158
self . getConfig ( ) . autoUpgradeAnonymousUsers ( ) &&
1160
1159
! self . getAuth ( ) . currentUser ) {
1161
- return ( firebase . Promise || goog . Promise ) . resolve ( self . currentUser_ ) ;
1160
+ return self . clearTempAuthState ( ) . then ( function ( ) {
1161
+ return self . currentUser_ ;
1162
+ } ) ;
1162
1163
} else if ( user ) {
1163
1164
// TODO: optimize and fail directly as this will fail in most cases
1164
1165
// with error credential already in use.
1165
1166
// There are cases where this is required. For example, when email
1166
1167
// mismatch occurs and the user continues with the new account.
1167
1168
return /** @type {!firebase.Promise<!firebase.User> } */ (
1168
- user . linkWithCredential ( credential )
1169
- . then ( function ( ) {
1169
+ self . clearTempAuthState ( ) . then ( function ( ) {
1170
+ return user . linkWithCredential ( credential ) ;
1171
+ } ) . then ( function ( ) {
1170
1172
return user ;
1171
1173
} , function ( error ) {
1172
1174
// Rethrow email already in use error so it can trigger the account
@@ -1183,7 +1185,84 @@ firebaseui.auth.AuthUI.prototype.finishSignInWithCredential =
1183
1185
// Finishes sign in with the supplied credential on the developer provided
1184
1186
// Auth instance. On completion, this will redirect to signInSuccessUrl or
1185
1187
// trigger the signInSuccess callback.
1186
- return self . getExternalAuth ( ) . signInWithCredential ( credential ) ;
1188
+ return self . clearTempAuthState ( ) . then ( function ( ) {
1189
+ return self . getExternalAuth ( ) . signInWithCredential ( credential ) ;
1190
+ } ) ;
1191
+ }
1192
+ } ;
1193
+ // Initialize current user if auto upgrade is enabled beforing running
1194
+ // callback and returning result.
1195
+ return this . initializeForAutoUpgrade_ ( cb ) ;
1196
+ } ;
1197
+
1198
+
1199
+ /**
1200
+ * Finishes FirebaseUI login with the given 3rd party credentials.
1201
+ * @param {!firebaseui.auth.AuthResult } authResult The Auth result.
1202
+ * @return {!firebase.Promise<!firebaseui.auth.AuthResult> }
1203
+ */
1204
+ firebaseui . auth . AuthUI . prototype . finishSignInAndRetrieveDataWithAuthResult =
1205
+ function ( authResult ) {
1206
+ // Check if instance is already destroyed.
1207
+ this . checkIfDestroyed_ ( ) ;
1208
+ var self = this ;
1209
+ var cb = function ( user ) {
1210
+ if ( self . currentUser_ &&
1211
+ ! self . currentUser_ [ 'isAnonymous' ] &&
1212
+ self . getConfig ( ) . autoUpgradeAnonymousUsers ( ) &&
1213
+ ! self . getAuth ( ) . currentUser ) {
1214
+ return self . clearTempAuthState ( ) . then ( function ( ) {
1215
+ // Do not expose password Auth credential to signInSuccess callback.
1216
+ if ( authResult [ 'credential' ] [ 'providerId' ] == 'password' ) {
1217
+ authResult [ 'credential' ] = null ;
1218
+ }
1219
+ return authResult ;
1220
+ } ) ;
1221
+ } else if ( user ) {
1222
+ // TODO: optimize and fail directly as this will fail in most cases
1223
+ // with error credential already in use.
1224
+ // There are cases where this is required. For example, when email
1225
+ // mismatch occurs and the user continues with the new account.
1226
+ return /** @type {!firebase.Promise<!firebaseui.auth.AuthResult> } */ (
1227
+ self . clearTempAuthState ( ) . then ( function ( ) {
1228
+ return user . linkAndRetrieveDataWithCredential (
1229
+ authResult [ 'credential' ] ) ;
1230
+ } ) . then ( function ( userCredential ) {
1231
+ authResult [ 'user' ] = userCredential [ 'user' ] ;
1232
+ authResult [ 'credential' ] = userCredential [ 'credential' ] ;
1233
+ authResult [ 'operationType' ] = userCredential [ 'operationType' ] ;
1234
+ authResult [ 'additionalUserInfo' ] =
1235
+ userCredential [ 'additionalUserInfo' ] ;
1236
+ return authResult ;
1237
+ } , function ( error ) {
1238
+ // Rethrow email already in use error so it can trigger the account
1239
+ // linking flow.
1240
+ if ( error &&
1241
+ error [ 'code' ] == 'auth/email-already-in-use' &&
1242
+ error [ 'email' ] && error [ 'credential' ] ) {
1243
+ throw error ;
1244
+ }
1245
+ // For all other errors, run onUpgrade check.
1246
+ return self . onUpgradeError ( error , authResult [ 'credential' ] ) ;
1247
+ } ) ) ;
1248
+ } else {
1249
+ // Finishes sign in with the supplied credential on the developer provided
1250
+ // Auth instance. On completion, this will redirect to signInSuccessUrl or
1251
+ // trigger the signInSuccessWithAuthResult callback.
1252
+ if ( ! authResult [ 'credential' ] ) {
1253
+ throw new Error ( 'No credential found!' ) ;
1254
+ }
1255
+ return self . clearTempAuthState ( ) . then ( function ( ) {
1256
+ return self . getExternalAuth ( ) . signInAndRetrieveDataWithCredential (
1257
+ authResult [ 'credential' ] ) ;
1258
+ } ) . then ( function ( userCredential ) {
1259
+ authResult [ 'user' ] = userCredential [ 'user' ] ;
1260
+ authResult [ 'credential' ] = userCredential [ 'credential' ] ;
1261
+ authResult [ 'operationType' ] = userCredential [ 'operationType' ] ;
1262
+ // AdditionalUserInfo should remain the same as isNewUser field should
1263
+ // be the one returned in the first sign in attempt.
1264
+ return authResult ;
1265
+ } ) ;
1187
1266
}
1188
1267
} ;
1189
1268
// Initialize current user if auto upgrade is enabled beforing running
0 commit comments