Android integrating biometric into your app

Let's say we would like to make a payment and to add abit of security to this, we will trigger a biometric to ensure a legit user is authorizing a payment. 

To start implementing, 

app/build.gradle.kts

dependencies {
implementation(libs.androidx.biometric)


And then we will update our MainActivity.kt with this before passing it to our Composable. This onAuthenticate is being passed down all the way to our composable and finally to our button click command 

@dagger.hilt.android.AndroidEntryPoint

class MainActivity : FragmentActivity() {
private val viewModel: HomeViewModel by viewModels()
private lateinit var biometricHelper: BiometricHelper

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
biometricHelper = BiometricHelper(this)
setContent {
GetMyHomeTheme {
GetMyHomeApp(
viewModel = viewModel,
onAuthenticate = { onSuccess ->
performBiometricAuth(onSuccess)
}
)
}
}
}

Our composable


@Composable
fun GetMyHomeApp(
viewModel: HomeViewModel,
onAuthenticate: (onSuccess: () -> Unit) -> Unit
) {
}

And further down our HomeUIComponentsBuild composable

@Composable
fun HomeUIComponentsBuild(
location: String,
onLocationChange: (String) -> Unit,
propertyType: String,
onPropertyTypeChange: (String) -> Unit,
useGraphQL: Int,
reportContent: String?,
viewModel: HomeViewModel,
onAuthenticate: (onSuccess: () -> Unit) -> Unit
) {

}

And this is our performBiometricAuth

private fun performBiometricAuth(onSuccess: () -> Unit) {
when (biometricHelper.canAuthenticate()) {
BiometricManager.BIOMETRIC_SUCCESS -> {
biometricHelper.showBiometricPrompt(
onSuccess = onSuccess,
onError = { errorCode, errString ->
Toast.makeText(this, "Auth error: $errString", Toast.LENGTH_SHORT).show()
},
onFailed = {
Toast.makeText(this, "Auth failed", Toast.LENGTH_SHORT).show()
}
)
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
Toast.makeText(this, "Biometrics not enrolled. Please configure in settings.", Toast.LENGTH_LONG).show()
biometricHelper.launchBiometricSettings()
}
else -> {
Toast.makeText(this, "Biometrics not available or supported", Toast.LENGTH_SHORT).show()
// If biometrics not available, should we allow or block?
// User said "if it is not setup prompt user to install/configure it, otherwise proceed with biometric validation"
// If it's literally NOT supported by hardware, maybe we just fallback to search?
// But for now let's just toast.
onSuccess() // Fallback if hardware doesn't support it at all? Or block?
// The prompt says "if it is not setup prompt user to install/configure it"
// So if it is error none enrolled, we prompt.
}
}
}

In our libs.versions.xml

[versions]
biometric = "1.2.0-alpha05"
[libraries]
androidx-biometric = { group = "androidx.biometric", name = "biometric", version.ref = "biometric" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }

Sample code can be found here

https://github.com/kepungnzai/biometric-payment


Comments

Popular posts from this blog

mongosh install properly

vllm : Failed to infer device type

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20