Skip to content

Instantly share code, notes, and snippets.

@DjangoLC
Created May 12, 2020 15:11
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 DjangoLC/74a7bd15af9769185564921a1f853986 to your computer and use it in GitHub Desktop.
Save DjangoLC/74a7bd15af9769185564921a1f853986 to your computer and use it in GitHub Desktop.
Clase que implementa la interfaz que data utiliza para hacer login con biometrics
package com.example.cleanarchme.data
import android.app.Application
import android.widget.Toast
import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity
import com.example.data.auth.Auth
import java.util.concurrent.Executor
class AuthImpl(private val context: Application, private var activity: FragmentActivity) : Auth {
private val executor = Executor { r -> r.run() }
override fun authWithFingerPrint(callback: (Boolean) -> Unit) {
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setDeviceCredentialAllowed(true)
.build()
val biometricPrompt = BiometricPrompt(
activity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Toast.makeText(context, "Authentication error: $errString", Toast.LENGTH_SHORT)
.show()
callback.invoke(false)
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
// User has verified the signature, cipher, or message
// authentication code (MAC) associated with the crypto object,
// so you can use it in your app's crypto-driven workflows.
Toast.makeText(context, "Authentication success: ", Toast.LENGTH_SHORT).show()
callback.invoke(true)
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(context, "Authentication failed", Toast.LENGTH_SHORT).show()
callback.invoke(false)
}
})
// Displays the "log in" prompt.
biometricPrompt.authenticate(promptInfo)
}
override suspend fun authWithFaceID(): Boolean {
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment