Skip to content

Commit 8aa67b1

Browse files
authored
Merge pull request #3 from anotherdev/pk/firebase-user-api
Enhance FirebaseUser API
2 parents fef33d0 + f586f87 commit 8aa67b1

File tree

15 files changed

+420
-36
lines changed

15 files changed

+420
-36
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ dependencies {
6060

6161
implementation "com.google.android.material:material:1.1.0"
6262

63-
implementation platform("com.google.firebase:firebase-bom:25.2.2")
63+
implementation platform("com.google.firebase:firebase-bom:$firebaseBomVersion")
6464
implementation "com.google.firebase:firebase-analytics"
6565
implementation "com.google.firebase:firebase-database"
6666

buildsystem/dependencies.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ext {
22
// Manifest version information!
33
def versionMajor = 0
44
def versionMinor = 1
5-
def versionPatch = 2
5+
def versionPatch = 3
66
def versionBuild = 0 // bump for dogfood builds, public betas, etc.
77

88
// Eko SDK version
@@ -28,6 +28,7 @@ ext {
2828
androidxAppCompatVersion = '1.1.0'
2929
androidxCoreKtxVersion = '1.2.0'
3030
androidxLifecycleVersion = '2.2.0'
31+
firebaseBomVersion = '25.3.1'
3132
gsonVersion = '2.8.6'
3233
immutablesVersion = '2.8.2'
3334
okhttpVersion = '4.4.1'

firebase-auth-rest/core/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dependencies {
7272

7373
api "com.google.code.gson:gson:$gsonVersion"
7474

75-
implementation platform("com.google.firebase:firebase-bom:25.2.2")
75+
implementation platform("com.google.firebase:firebase-bom:$firebaseBomVersion")
7676
implementation "com.google.firebase:firebase-auth-interop:19.0.0"
7777
api "com.google.firebase:firebase-common"
7878
implementation "com.google.firebase:firebase-core"

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import androidx.annotation.Nullable;
55

66
import com.anotherdev.firebase.auth.provider.AuthCredential;
7+
import com.google.gson.JsonObject;
78

9+
import java.util.List;
10+
11+
import io.reactivex.rxjava3.annotations.CheckReturnValue;
812
import io.reactivex.rxjava3.core.Completable;
913
import io.reactivex.rxjava3.core.Single;
1014

@@ -15,6 +19,9 @@ public interface FirebaseUser {
1519
@Nullable
1620
String getIdToken();
1721

22+
@NonNull
23+
JsonObject getUserInfo();
24+
1825
@Nullable
1926
String getUid();
2027

@@ -25,14 +32,27 @@ public interface FirebaseUser {
2532
String getEmail();
2633

2734
@NonNull
35+
List<UserInfo> getProviderData();
36+
37+
boolean isAnonymous();
38+
39+
@NonNull
40+
@CheckReturnValue
2841
Single<SignInResponse> linkWithCredential(@NonNull AuthCredential credential);
2942

3043
@NonNull
44+
@CheckReturnValue
3145
Single<SignInResponse> reauthenticate(@NonNull AuthCredential credential);
3246

3347
@NonNull
48+
@CheckReturnValue
49+
Completable reload();
50+
51+
@NonNull
52+
@CheckReturnValue
3453
Completable updateProfile(@NonNull UserProfileChangeRequest request);
3554

3655
@NonNull
56+
@CheckReturnValue
3757
Completable updatePassword(@NonNull String newPassword);
3858
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.anotherdev.firebase.auth;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import org.immutables.gson.Gson;
6+
import org.immutables.value.Value;
7+
8+
@Value.Immutable
9+
@Value.Style(strictBuilder = true)
10+
@Gson.TypeAdapters
11+
public interface UserInfo {
12+
13+
@SerializedName("rawId")
14+
String getUid();
15+
16+
@Value.Default
17+
@SerializedName("displayName")
18+
default String getDisplayName() {
19+
return "";
20+
}
21+
22+
@Value.Default
23+
@SerializedName("email")
24+
default String getEmail() {
25+
return "";
26+
}
27+
28+
@Value.Default
29+
@SerializedName("photoUrl")
30+
default String getPhotoUrl() {
31+
return "";
32+
}
33+
34+
@Value.Default
35+
@SerializedName("providerId")
36+
default String getProviderId() {
37+
return "";
38+
}
39+
}

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import androidx.annotation.NonNull;
77

88
import com.anotherdev.firebase.auth.data.model.FirebaseUserImpl;
9+
import com.anotherdev.firebase.auth.data.model.UserProfile;
10+
import com.anotherdev.firebase.auth.init.AppContext;
911
import com.anotherdev.firebase.auth.util.FarGson;
1012
import com.f2prateek.rx.preferences2.Preference;
1113
import com.f2prateek.rx.preferences2.RxSharedPreferences;
@@ -15,37 +17,40 @@ public class Data {
1517

1618
private static final Gson GSON = FarGson.get();
1719

18-
private final RxSharedPreferences rxSharedPreferences;
20+
private static class DataHolder {
21+
private static final SharedPreferences SHARED_PREFERENCES;
22+
private static final RxSharedPreferences RX_SHARED_PREFERENCES;
23+
24+
static {
25+
Context context = AppContext.get();
26+
SHARED_PREFERENCES = context.getSharedPreferences(Data.class.getName(), Context.MODE_PRIVATE);
27+
RX_SHARED_PREFERENCES = RxSharedPreferences.create(SHARED_PREFERENCES);
28+
}
29+
}
1930

2031

2132
private Data(Context context) {
22-
SharedPreferences pref = context.getSharedPreferences(Data.class.getName(), Context.MODE_PRIVATE);
23-
rxSharedPreferences = RxSharedPreferences.create(pref);
2433
}
2534

2635
public Preference<String> getApiKey() {
27-
return rxSharedPreferences.getString("google_api_key", "");
36+
return DataHolder.RX_SHARED_PREFERENCES.getString("google_api_key", "");
2837
}
2938

3039
public Preference<FirebaseUserImpl> getCurrentUser(@NonNull FirebaseUserImpl defaultValue) {
31-
return rxSharedPreferences.getObject(
40+
return DataHolder.RX_SHARED_PREFERENCES.getObject(
3241
"current_user_info",
3342
defaultValue,
34-
new Preference.Converter<FirebaseUserImpl>() {
35-
@NonNull
36-
@Override
37-
public FirebaseUserImpl deserialize(@NonNull String json) {
38-
return GSON.fromJson(json, FirebaseUserImpl.class);
39-
}
40-
41-
@NonNull
42-
@Override
43-
public String serialize(@NonNull FirebaseUserImpl user) {
44-
return GSON.toJson(user);
45-
}
46-
});
43+
new RxPreferenceGsonConverter<>(FirebaseUserImpl.class));
4744
}
4845

46+
public Preference<UserProfile> getUserProfile(@NonNull final String uid) {
47+
return DataHolder.RX_SHARED_PREFERENCES.getObject(
48+
uid,
49+
UserProfile.builder().localId(uid).build(),
50+
new RxPreferenceGsonConverter<>(UserProfile.class));
51+
}
52+
53+
4954
public static Data from(Context context) {
5055
return new Data(context);
5156
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.anotherdev.firebase.auth.data;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.anotherdev.firebase.auth.util.FarGson;
6+
import com.f2prateek.rx.preferences2.Preference;
7+
import com.google.gson.Gson;
8+
9+
class RxPreferenceGsonConverter<T> implements Preference.Converter<T> {
10+
11+
private final Gson GSON = FarGson.get();
12+
private final Class<T> valueType;
13+
14+
15+
RxPreferenceGsonConverter(Class<T> valueType) {
16+
this.valueType = valueType;
17+
}
18+
19+
@NonNull
20+
@Override
21+
public T deserialize(@NonNull String json) {
22+
return GSON.fromJson(json, valueType);
23+
}
24+
25+
@NonNull
26+
@Override
27+
public String serialize(@NonNull T value) {
28+
return GSON.toJson(value);
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.anotherdev.firebase.auth.data.model;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
7+
@SuppressWarnings("WeakerAccess")
8+
class FirebaseAuthData {
9+
10+
@SerializedName("identities")
11+
FirebaseAuthIdentities identities = new FirebaseAuthIdentities();
12+
13+
@SerializedName("sign_in_provider")
14+
String signInProvider = "unknown";
15+
16+
17+
FirebaseAuthData() {
18+
}
19+
20+
@NonNull
21+
public FirebaseAuthIdentities getIdentities() {
22+
return identities;
23+
}
24+
25+
@NonNull
26+
public String getSignInProvider() {
27+
return signInProvider;
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.anotherdev.firebase.auth.data.model;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
@SuppressWarnings("WeakerAccess")
11+
class FirebaseAuthIdentities {
12+
13+
@SerializedName("google.com")
14+
List<String> google = Collections.emptyList();
15+
16+
@SerializedName("facebook.com")
17+
List<String> facebook = Collections.emptyList();
18+
19+
@SerializedName("email")
20+
List<String> email = Collections.emptyList();
21+
22+
23+
@NonNull
24+
public List<String> getGoogle() {
25+
return google;
26+
}
27+
28+
@NonNull
29+
public List<String> getFacebook() {
30+
return facebook;
31+
}
32+
33+
@NonNull
34+
public List<String> getEmail() {
35+
return email;
36+
}
37+
38+
public boolean isEmpty() {
39+
return google.isEmpty()
40+
&& facebook.isEmpty()
41+
&& email.isEmpty();
42+
}
43+
}

0 commit comments

Comments
 (0)