Permalink
Browse files

Make google-secrets.json optional (#785)

  • Loading branch information...
1 parent 472b458 commit 39e3c371b5915f563800271ea0d96e2010b86215 @phil-lopreiato phil-lopreiato committed on GitHub Oct 8, 2016
@@ -2,13 +2,15 @@
import com.google.firebase.auth.FirebaseAuth;
+import com.thebluealliance.androidclient.TbaLogger;
import com.thebluealliance.androidclient.accounts.AccountController;
import com.thebluealliance.androidclient.accounts.AccountModule;
import com.thebluealliance.androidclient.auth.firebase.FirebaseAuthProvider;
import com.thebluealliance.androidclient.auth.google.GoogleAuthProvider;
import android.content.Context;
+import javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Singleton;
@@ -24,9 +26,17 @@ public AuthModule(Context context) {
mContext = context;
}
- @Provides @Singleton
+ @Provides @Singleton @Nullable
public FirebaseAuth provideFirebaseAuth() {
- return FirebaseAuth.getInstance();
+ try {
+ return FirebaseAuth.getInstance();
+ } catch (IllegalStateException ex) {
+ /* When there is no google-secrets.json file found, the library throws an exception
+ * here which causes insta-crashes for us. Silently recover here...
+ */
+ TbaLogger.w("Unable to find google-secrets.json, disabling login");
+ return null;
+ }
}
@Provides
@@ -35,8 +45,8 @@ public GoogleAuthProvider provideGoogleAuthProvider(AccountController accountCon
}
@Provides @Named("firebase_auth")
- public AuthProvider provideFirebaseAuthProvider(FirebaseAuth firebaseAuth,
- GoogleAuthProvider googleAuthProvider) {
+ public AuthProvider provideFirebaseAuthProvider(@Nullable FirebaseAuth firebaseAuth,
+ GoogleAuthProvider googleAuthProvider) {
return new FirebaseAuthProvider(firebaseAuth, googleAuthProvider);
}
}
@@ -5,14 +5,14 @@
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
-import com.firebase.client.annotations.Nullable;
import com.thebluealliance.androidclient.auth.AuthProvider;
import com.thebluealliance.androidclient.auth.User;
import com.thebluealliance.androidclient.auth.google.GoogleAuthProvider;
import com.thebluealliance.androidclient.auth.google.GoogleSignInUser;
import android.content.Intent;
+import javax.annotation.Nullable;
import javax.inject.Singleton;
import rx.Observable;
@@ -21,10 +21,12 @@
@Singleton
public class FirebaseAuthProvider implements AuthProvider {
- private final FirebaseAuth mFirebaseAuth;
+ private final @Nullable FirebaseAuth mFirebaseAuth;
private final GoogleAuthProvider mGoogleAuthProvider;
- public FirebaseAuthProvider(FirebaseAuth firebaseAuth, GoogleAuthProvider googleProvider) {
+ public FirebaseAuthProvider(
+ @Nullable FirebaseAuth firebaseAuth,
+ GoogleAuthProvider googleProvider) {
mFirebaseAuth = firebaseAuth;
mGoogleAuthProvider = googleProvider;
}
@@ -41,11 +43,14 @@ public void onStop() {
@Override
public boolean isUserSignedIn() {
- return mFirebaseAuth.getCurrentUser() != null;
+ return mFirebaseAuth != null && mFirebaseAuth.getCurrentUser() != null;
}
@Override @Nullable
public User getCurrentUser() {
+ if (mFirebaseAuth == null) {
+ return null;
+ }
FirebaseUser firebaseUser = mFirebaseAuth.getCurrentUser();
if (firebaseUser == null) {
return null;
@@ -63,7 +68,7 @@ public Intent buildSignInIntent() {
public Observable<FirebaseSignInUser> userFromSignInResult(int requestCode, int resultCode, Intent data) {
Observable<? extends User> googleUser = mGoogleAuthProvider.userFromSignInResult(requestCode, resultCode, data);
return googleUser.switchMap(user -> {
- if (!(user instanceof GoogleSignInUser)) {
+ if (mFirebaseAuth == null || !(user instanceof GoogleSignInUser)) {
return Observable.empty();
}
@@ -93,7 +98,7 @@ public void call(Subscriber<? super FirebaseSignInUser> subscriber) {
public Observable<FirebaseSignInUser> signInLegacyUser() {
Observable<? extends User> googleUser = mGoogleAuthProvider.signInLegacyUser();
return googleUser.switchMap(user -> {
- if (!(user instanceof GoogleSignInUser)) {
+ if (mFirebaseAuth == null || !(user instanceof GoogleSignInUser)) {
return Observable.empty();
}
@@ -33,7 +33,7 @@ public void onCreate() {
@Override
protected void onHandleIntent(Intent intent) {
TbaLogger.d("Trying to migrate legacy auth to Firebase");
- User user = mAuthProvider.signInLegacyUser().toBlocking().first();
+ User user = mAuthProvider.signInLegacyUser().toBlocking().firstOrDefault(null);
if (user != null) {
TbaLogger.d("Migrated user");

0 comments on commit 39e3c37

Please sign in to comment.