@@ -911,7 +911,7 @@ firebaseui.auth.AuthUI.prototype.startSignInWithEmailAndPassword =
911911 * Create a new email and password account.
912912 * @param {string } email The email to sign up with.
913913 * @param {string } password The password to sign up with.
914- * @return {!firebase.Promise<!firebase.User > }
914+ * @return {!firebase.Promise<!firebase.auth.UserCredential > }
915915 */
916916firebaseui . auth . AuthUI . prototype . startCreateUserWithEmailAndPassword =
917917 function ( email , password ) {
@@ -925,17 +925,15 @@ firebaseui.auth.AuthUI.prototype.startCreateUserWithEmailAndPassword =
925925 // For anonymous user upgrade, call link with credential on the external
926926 // Auth user. Otherwise merge conflict will always occur when linking the
927927 // 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 ) ) ;
933930 } else {
934931 // Start create user with email and password. This runs on the internal
935932 // Auth instance as finish sign in will sign in with that same credential
936933 // 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 ) ) ;
939937 }
940938 } ;
941939 // Initialize current user if auto upgrade is enabled beforing running
@@ -1149,24 +1147,28 @@ firebaseui.auth.AuthUI.prototype.finishSignInWithCredential =
11491147 this . checkIfDestroyed_ ( ) ;
11501148 var self = this ;
11511149 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.
11551153 // 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.
11571156 if ( self . currentUser_ &&
11581157 ! self . currentUser_ [ 'isAnonymous' ] &&
11591158 self . getConfig ( ) . autoUpgradeAnonymousUsers ( ) &&
11601159 ! self . getAuth ( ) . currentUser ) {
1161- return ( firebase . Promise || goog . Promise ) . resolve ( self . currentUser_ ) ;
1160+ return self . clearTempAuthState ( ) . then ( function ( ) {
1161+ return self . currentUser_ ;
1162+ } ) ;
11621163 } else if ( user ) {
11631164 // TODO: optimize and fail directly as this will fail in most cases
11641165 // with error credential already in use.
11651166 // There are cases where this is required. For example, when email
11661167 // mismatch occurs and the user continues with the new account.
11671168 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 ( ) {
11701172 return user ;
11711173 } , function ( error ) {
11721174 // Rethrow email already in use error so it can trigger the account
@@ -1183,7 +1185,84 @@ firebaseui.auth.AuthUI.prototype.finishSignInWithCredential =
11831185 // Finishes sign in with the supplied credential on the developer provided
11841186 // Auth instance. On completion, this will redirect to signInSuccessUrl or
11851187 // 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+ } ) ;
11871266 }
11881267 } ;
11891268 // Initialize current user if auto upgrade is enabled beforing running
0 commit comments