Skip to content

Instantly share code, notes, and snippets.

@anotherdev
Last active April 2, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anotherdev/40181800d38e2de0e9afa50c6d4e12d0 to your computer and use it in GitHub Desktop.
Save anotherdev/40181800d38e2de0e9afa50c6d4e12d0 to your computer and use it in GitHub Desktop.
Firebase Auth REST SDK Android integration
Requirements:
1. Minimum Android API 21
2. Enable Java 8
Steps:
@anotherdev
Copy link
Author

Setup the Firebase Auth REST SDK

  1. Add the JitPack repository to your root build file
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
  1. Add the dependency in your app module build file
dependencies {
  implementation "com.github.anotherdev:firebase-auth-rest:x.y.z"
}

Where x.y.z = latest Firebase Auth REST SDK on

@anotherdev
Copy link
Author

Example project: firebase-auth-rest

@anotherdev
Copy link
Author

Steps:

  1. Replace com.google.firebase.auth.FirebaseAuth with com.anotherdev.firebase.auth.common.FirebaseAuth.
FirebaseApp app = FirebaseApp.getInstance();
com.anotherdev.firebase.auth.common.FirebaseAuth auth = FirebaseAuthRest.getInstance(app);
  1. Sign in anonymously function:
auth.signInAnonymously().subscribe();
  1. Facebook Login function:
com.facebook.login.LoginResult fb;
String token = fb.getAccessToken().getToken();
// https://console.firebase.google.com/u/0/project/<your-app>/authentication/providers -> click on Facebook
String oAuthRedirectUri = "https://<your-app>.firebaseapp.com/__/auth/handler";
AuthCredential credential = com.anotherdev.firebase.auth.provider.FacebookAuthProvider.getCredential(token, oAuthRedirectUri);
auth.signInWithCredential(credential).subscribe();
  1. Google Sign-In function:
GoogleSignInClient google;
startActivityForResult(google.getSignInIntent(), REQ_GOOGLE_SIGN_IN);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    GoogleSignInAccount acc = GoogleSignIn.getSignedInAccountFromIntent(data).getResult();
    AuthCredential credential = com.anotherdev.firebase.auth.provider.GoogleAuthProvider.getCredential(acc.getIdToken());
    auth.signInWithCredential(credential).subscribe();
}
  1. Listen to user login/logout:
auth.authStateChanges()
    .doOnNext(auth -> Log.i("FAR", "user auth state changed!"))
    .subscribe();
  1. Logout function:
auth.signOut();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment