1010import com .anotherdev .firebase .auth .SignInResponse ;
1111import com .anotherdev .firebase .auth .data .Data ;
1212import com .anotherdev .firebase .auth .data .model .FirebaseUserImpl ;
13+ import com .anotherdev .firebase .auth .data .model .UserProfile ;
1314import com .anotherdev .firebase .auth .provider .IdpAuthCredential ;
1415import com .anotherdev .firebase .auth .rest .api .RestAuthApi ;
1516import com .anotherdev .firebase .auth .rest .api .model .ExchangeTokenRequest ;
17+ import com .anotherdev .firebase .auth .rest .api .model .GetAccountInfoRequest ;
18+ import com .anotherdev .firebase .auth .rest .api .model .GetAccountInfoResponse ;
1619import com .anotherdev .firebase .auth .rest .api .model .ImmutableSignInWithIdpRequest ;
1720import com .anotherdev .firebase .auth .rest .api .model .SendPasswordResetEmailRequest ;
1821import com .anotherdev .firebase .auth .rest .api .model .SendPasswordResetEmailResponse ;
@@ -93,7 +96,8 @@ public Observable<FirebaseAuth> authStateChanges() {
9396 public Single <SignInResponse > signInAnonymously () {
9497 return RestAuthApi .auth ()
9598 .signInAnonymously (SignInAnonymouslyRequest .builder ().build ())
96- .map (this ::saveCurrentUser );
99+ .map (this ::saveCurrentUser )
100+ .map (this ::getAccountInfo );
97101 }
98102
99103 @ NonNull
@@ -105,7 +109,8 @@ public Single<SignInResponse> createUserWithEmailAndPassword(@NonNull String ema
105109 .build ();
106110 return RestAuthApi .auth ()
107111 .createUserWithEmailAndPassword (request )
108- .map (this ::saveCurrentUser );
112+ .map (this ::saveCurrentUser )
113+ .map (this ::getAccountInfo );
109114 }
110115
111116 @ NonNull
@@ -117,7 +122,8 @@ public Single<SignInResponse> signInWithEmailAndPassword(@NonNull String email,
117122 .build ();
118123 return RestAuthApi .auth ()
119124 .signInWithEmailAndPassword (request )
120- .map (this ::saveCurrentUser );
125+ .map (this ::saveCurrentUser )
126+ .map (this ::getAccountInfo );
121127 }
122128
123129 @ NonNull
@@ -135,7 +141,8 @@ public Single<SignInResponse> signInWithCustomToken(@NonNull String customToken)
135141 .build ();
136142 return RestAuthApi .auth ()
137143 .signInWithCustomToken (request )
138- .map (this ::saveCurrentUser );
144+ .map (this ::saveCurrentUser )
145+ .map (this ::getAccountInfo );
139146 }
140147
141148 @ NonNull
@@ -154,7 +161,8 @@ private Single<SignInResponse> performSignInWithCredential(ImmutableSignInWithId
154161 .build ();
155162 return RestAuthApi .auth ()
156163 .signInWithCredential (request )
157- .map (this ::saveCurrentUser );
164+ .map (this ::saveCurrentUser )
165+ .map (this ::getAccountInfo );
158166 }
159167
160168 @ NonNull
@@ -245,4 +253,19 @@ private void saveCurrentUser(String idToken, String refreshToken) throws Firebas
245253 l .onIdTokenChanged (new InternalTokenResult (idToken ));
246254 }
247255 }
256+
257+ private SignInResponse getAccountInfo (SignInResponse signInResponse ) {
258+ GetAccountInfoRequest request = GetAccountInfoRequest .builder ()
259+ .idToken (signInResponse .getIdToken ())
260+ .build ();
261+ GetAccountInfoResponse accountInfo = RestAuthApi .auth ()
262+ .getAccounts (request ).blockingGet ();
263+
264+ Data dataStore = Data .from (app .getApplicationContext ());
265+ for (UserProfile profile : accountInfo .getUsers ()) {
266+ final String uid = profile .getLocalId ();
267+ dataStore .getUserProfile (uid ).set (profile );
268+ }
269+ return signInResponse ;
270+ }
248271}
0 commit comments