Skip to content

Commit 9233d63

Browse files
committed
get account info / user profile right after sign in
1 parent 2ba2738 commit 9233d63

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

firebase-auth-rest/core/src/main/java/com/anotherdev/firebase/auth/FirebaseUser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.anotherdev.firebase.auth.provider.AuthCredential;
77
import com.google.gson.JsonObject;
88

9+
import java.util.List;
10+
911
import io.reactivex.rxjava3.core.Completable;
1012
import io.reactivex.rxjava3.core.Single;
1113

@@ -28,6 +30,9 @@ public interface FirebaseUser {
2830
@Nullable
2931
String getEmail();
3032

33+
@NonNull
34+
List<UserInfo> getProviderData();
35+
3136
boolean isAnonymous();
3237

3338
@NonNull

firebase-auth-rest/core/src/main/java/com/anotherdev/firebase/auth/data/model/FirebaseUserImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.anotherdev.firebase.auth.FirebaseUser;
1111
import com.anotherdev.firebase.auth.ImmutableUserProfileChangeRequest;
1212
import com.anotherdev.firebase.auth.SignInResponse;
13+
import com.anotherdev.firebase.auth.UserInfo;
1314
import com.anotherdev.firebase.auth.UserProfileChangeRequest;
15+
import com.anotherdev.firebase.auth.data.Data;
1416
import com.anotherdev.firebase.auth.provider.AuthCredential;
1517
import com.anotherdev.firebase.auth.provider.EmailAuthCredential;
1618
import com.anotherdev.firebase.auth.provider.IdpAuthCredential;
@@ -23,6 +25,8 @@
2325
import com.google.gson.JsonElement;
2426
import com.google.gson.JsonObject;
2527

28+
import java.util.List;
29+
2630
import io.reactivex.rxjava3.core.Completable;
2731
import io.reactivex.rxjava3.core.Single;
2832
import timber.log.Timber;
@@ -45,6 +49,7 @@ public class FirebaseUserImpl implements FirebaseUser {
4549

4650
@Nullable
4751
transient FirebaseAuth auth;
52+
transient Data data;
4853

4954

5055
FirebaseUserImpl() {
@@ -80,6 +85,13 @@ private FirebaseAuth getAuth() {
8085
return auth;
8186
}
8287

88+
public Data getData() {
89+
if (data == null) {
90+
data = Data.from(getAuth().getApp().getApplicationContext());
91+
}
92+
return data;
93+
}
94+
8395
@NonNull
8496
private RestAuthProvider getAuthInternal() throws ClassCastException {
8597
return (RestAuthProvider) getAuth();
@@ -135,6 +147,15 @@ public String getEmail() {
135147
return getAsString(userInfo, "email");
136148
}
137149

150+
@NonNull
151+
@Override
152+
public List<UserInfo> getProviderData() {
153+
String uid = getUid();
154+
return getData().getUserProfile(uid != null ? uid : "")
155+
.get()
156+
.providerUserInfo();
157+
}
158+
138159
@Override
139160
public boolean isAnonymous() {
140161
return firebaseAuthData.getIdentities().isEmpty();

firebase-auth-rest/core/src/main/java/com/anotherdev/firebase/auth/rest/RestAuthProvider.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import com.anotherdev.firebase.auth.SignInResponse;
1111
import com.anotherdev.firebase.auth.data.Data;
1212
import com.anotherdev.firebase.auth.data.model.FirebaseUserImpl;
13+
import com.anotherdev.firebase.auth.data.model.UserProfile;
1314
import com.anotherdev.firebase.auth.provider.IdpAuthCredential;
1415
import com.anotherdev.firebase.auth.rest.api.RestAuthApi;
1516
import 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;
1619
import com.anotherdev.firebase.auth.rest.api.model.ImmutableSignInWithIdpRequest;
1720
import com.anotherdev.firebase.auth.rest.api.model.SendPasswordResetEmailRequest;
1821
import 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

Comments
 (0)