Skip to content

Instantly share code, notes, and snippets.

@dllewellyn
Created July 2, 2020 08:02
Show Gist options
  • Save dllewellyn/38d317e7ead44261aab600dfa7564a8d to your computer and use it in GitHub Desktop.
Save dllewellyn/38d317e7ead44261aab600dfa7564a8d to your computer and use it in GitHub Desktop.
private val oneTapClient: SignInClient by lazy {
Identity.getSignInClient(this)
}
private val loginResult =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
val credential = oneTapClient.getSignInCredentialFromIntent(it.data)
val idToken = credential.googleIdToken
val username = credential.id
val password = credential.password
when {
idToken != null -> {
// Got an ID token from Google. Use it to authenticate
// with your backend.
Log.d(TAG, "Got ID token.")
}
password != null -> {
// Got a saved username and password. Use them to authenticate
// with your backend.
Log.d(TAG, "Got password.")
}
else -> {
// Shouldn't happen.
Log.d(TAG, "No ID token or password!")
}
}
}
private val signUpResult =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
try {
val credential = oneTapClient.getSignInCredentialFromIntent(it.data)
val idToken = credential.googleIdToken
when {
idToken != null -> {
// Got an ID token from Google. Use it to authenticate
// with your backend.
Log.d(TAG, "Got ID token.")
}
else -> {
// Shouldn't happen.
Log.d(TAG, "No ID token!")
}
}
} catch (e: Exception) {
Log.e(TAG, e.message, e)
Log.v(TAG, "Hash is: ${getCertificateSHA1Fingerprint()}")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_one_tap_authentication)
signin()
}
private fun signin() {
lifecycleScope.launch(errorHandler { signup() }) {
val result = oneTapClient.suspendBeginSignInRequest(buildSignInRequest())
loginResult.launch(
IntentSenderRequest.Builder(result.pendingIntent)
.build()
)
}
}
private fun buildSignInRequest() = BeginSignInRequest.builder()
.setPasswordRequestOptions(
BeginSignInRequest.PasswordRequestOptions.builder()
.setSupported(true)
.build()
)
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
// Your server's client ID, not your Android client ID.
.setServerClientId(getString(R.string.one_tap_client_id))
// Only show accounts previously used to sign in.
.setFilterByAuthorizedAccounts(true)
.build()
)
.build()
private fun signup() {
lifecycleScope.launch(errorHandler()) {
val result = oneTapClient.suspendBeginSignInRequest(buildSignupRequest())
signUpResult.launch(
IntentSenderRequest.Builder(result.pendingIntent)
.build()
)
}
}
private fun errorHandler(onError: (() -> Unit)? = null): CoroutineExceptionHandler {
return CoroutineExceptionHandler { _, e ->
onError?.invoke()
Log.e(
TAG,
e.localizedMessage ?: "",
e
)
}
}
private fun buildSignupRequest() = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(getString(R.string.one_tap_client_id))
.setFilterByAuthorizedAccounts(false)
.build()
)
.build()
companion object {
const val TAG = "OneTapAuthentication"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment