Last active
June 4, 2020 14:00
-
-
Save AngaKoko/8023b79633d5d5f1561388f5d60a358e to your computer and use it in GitHub Desktop.
Firebase authentication using Googe sign in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); | |
if (requestCode == RC_SIGN_IN) { | |
val task = GoogleSignIn.getSignedInAccountFromIntent(data) | |
try { | |
// Google Sign In was successful, authenticate with Firebase | |
val account = task.getResult(ApiException::class.java) | |
firebaseAuthWithGoogle(account!!) | |
} catch (e: ApiException) { | |
// Google Sign In failed, update UI appropriately | |
Log.w(TAG, "Google sign in failed", e) | |
// ... | |
} | |
} | |
} | |
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) { | |
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.id!!) | |
val credential = GoogleAuthProvider.getCredential(acct.idToken, null) | |
auth.signInWithCredential(credential) | |
.addOnCompleteListener(this) { task -> | |
if (task.isSuccessful) { | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "signInWithCredential:success") | |
val user = auth.currentUser | |
//update ui or navigate to another activity | |
updateUI(user) | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "signInWithCredential:failure", task.exception) | |
updateUI(null) | |
} | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment