Created
May 12, 2020 15:11
-
-
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
This file contains 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
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