Skip to content

Instantly share code, notes, and snippets.

@crowelch
Created September 28, 2017 01:54
Show Gist options
  • Select an option

  • Save crowelch/aef4c138f7877738a0153bace35260fb to your computer and use it in GitHub Desktop.

Select an option

Save crowelch/aef4c138f7877738a0153bace35260fb to your computer and use it in GitHub Desktop.
Google Sign In implemented in Kotlin
class MainActivity : AppCompatActivity(), GoogleApiClient.OnConnectionFailedListener {
private val RC_SIGN_IN = 9001
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding : MainActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val googleSignInOptions : GoogleSignInOptions = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()
val googleApiClient = GoogleApiClient
.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build()
binding.signInButton.setOnClickListener({
val intent : Intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient)
startActivityForResult(intent, RC_SIGN_IN)
})
}
override fun onConnectionFailed(p0: ConnectionResult) {
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == RC_SIGN_IN) {
val result : GoogleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
handleSignInResult(result)
}
}
private fun handleSignInResult(signInResult: GoogleSignInResult) {
if(signInResult.isSuccess) {
// Authenticated
val account : GoogleSignInAccount? = signInResult.signInAccount
} else {
// Failed
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment