Skip to content

Instantly share code, notes, and snippets.

@ihimanshurawat
Created June 3, 2018 06:51
Show Gist options
  • Save ihimanshurawat/6a7f136935253fc35109e401b5ac65b4 to your computer and use it in GitHub Desktop.
Save ihimanshurawat/6a7f136935253fc35109e401b5ac65b4 to your computer and use it in GitHub Desktop.
class Login : AppCompatActivity() {
//Google Login Request Code
private val RC_SIGN_IN = 7
//Google Sign In Client
private lateinit var mGoogleSignInClient: GoogleSignInClient
//Firebase Auth
private lateinit var mAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
mAuth = FirebaseAuth.getInstance()
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleSignInClient = GoogleSignIn.getClient(this,gso)
sign_in_button.setOnClickListener({
signIn()
})
}
private fun signIn() {
val signInIntent = mGoogleSignInClient.getSignInIntent()
startActivityForResult(signInIntent, RC_SIGN_IN)
}
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("Login", "Google sign in failed", e)
// ...
}
}
}
public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = mAuth.currentUser
updateUI(currentUser)
}
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
Log.d("Login", "firebaseAuthWithGoogle:" + acct.id!!)
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d("Login", "signInWithCredential:success")
val user = mAuth.currentUser
updateUI(user)
} else {
// If sign in fails, display a message to the user.
Log.w("Login", "signInWithCredential:failure", task.exception)
Toast.makeText(this,"Auth Failed",Toast.LENGTH_LONG).show()
updateUI(null)
}
// ...
}
}
fun updateUI(user: FirebaseUser?){
if(user != null){
//Do your Stuff
Toast.makeText(this,"Hello ${user.displayName}",Toast.LENGTH_LONG).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment