Created
September 28, 2017 01:54
-
-
Save crowelch/aef4c138f7877738a0153bace35260fb to your computer and use it in GitHub Desktop.
Google Sign In implemented in Kotlin
This file contains hidden or 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
| 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