Skip to content

Instantly share code, notes, and snippets.

@vprabhu
Created August 17, 2018 14:18
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 vprabhu/3fcf6e32abf7235a3bee6789163ba8ae to your computer and use it in GitHub Desktop.
Save vprabhu/3fcf6e32abf7235a3bee6789163ba8ae to your computer and use it in GitHub Desktop.
package com.example.firebaseAuth
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.google.android.gms.auth.api.Auth
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_login.*
class LoginActivity : AppCompatActivity() ,GoogleApiClient.OnConnectionFailedListener {
private val RC_SIGN_IN = 100
private lateinit var mAuth: FirebaseAuth
private lateinit var mGoogleSignInOptions: GoogleSignInOptions
private lateinit var mGoogleApiClient:GoogleApiClient
private var isUserLoggedIn = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
// initialise the FirebaseAuth instance
mAuth = FirebaseAuth.getInstance()
// Configure Google Sign In
mGoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
// configure the GoogleApiClient
mGoogleApiClient = GoogleApiClient
.Builder(this)
.enableAutoManage(this , this).addApi(Auth.GOOGLE_SIGN_IN_API , mGoogleSignInOptions).build()
button_login.setOnClickListener {
if(isUserLoggedIn){
doLogout()
}else{
doLogin()
}
}
}
public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = mAuth.currentUser
val userName = currentUser?.displayName
textView_username.text = userName
if(!userName.isNullOrEmpty()){
setLoginUIVisible(currentUser?.photoUrl)
}
}
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 result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
if (result.isSuccess) {
// Google Sign In was successful, authenticate with Firebase
val account = result.signInAccount
firebaseAuthWithGoogle(account!!)
} else {
// Google Sign In failed
Log.e("MainActivity", "Google Sign In failed.")
}
}
}
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
Log.d("Login", "firebaseAuthWithGoogle:" + acct.id!!)
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, OnCompleteListener<AuthResult> { task ->
Log.d("Login", "signInWithCredential:onComplete:" + task.isSuccessful)
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful) {
Log.d("Login", "signInWithCredential", task.exception)
Toast.makeText(this@LoginActivity, "Authentication failed.",
Toast.LENGTH_SHORT).show()
} else {
setLoginUIVisible(task.result?.user?.photoUrl)
textView_username.text = task.result?.user?.displayName
// textView_username.text = task.result?.user?.photoUrl.toString()
Glide.with(this@LoginActivity)
.load(task.result?.user?.photoUrl)
.into(imageView_profilePic)
setLogoutUI()
}
})
}
override fun onConnectionFailed(p0: ConnectionResult) {
Log.d("Login", "onConnectionFailed:$p0")
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show()
}
private fun doLogin(){
val signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
startActivityForResult(signInIntent, RC_SIGN_IN)
}
private fun doLogout(){
mAuth.signOut()
setLoginUIGone()
isUserLoggedIn = false
}
private fun setLogoutUI(){
isUserLoggedIn = true
}
private fun setLoginUIVisible(photoUrl: Uri?) {
textView_username.visibility = View.VISIBLE
imageView_profilePic.visibility = View.VISIBLE
Glide.with(this@LoginActivity)
.load(photoUrl)
.into(imageView_profilePic)
}
private fun setLoginUIGone(){
textView_username.visibility = View.GONE
imageView_profilePic.visibility = View.GONE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment