icerockdev / moko-biometry

Biometry authentication with Touch ID, Face ID from common code with Kotlin Multiplatform Mobile
https://moko.icerock.dev
Apache License 2.0
47 stars 6 forks source link

Desktop support #27

Open omarzer0 opened 11 months ago

omarzer0 commented 11 months ago

At first thx for your awesome libraries and great effort.

I'm using Biometry Auth in my KMP app and worked fine until I added Desktop support. I get this error

No matching variant of dev.icerock.moko:biometry-compose:0.4.0 was found. The consumer was configured to find a library for use during compile-time, preferably optimized for standard JVMs, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'debugApiElements-published' capability dev.icerock.moko:biometry-compose:0.4.0 declares a library for use during compile-time:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'

I know that Desktop has no touch or face ID but is there a way to exclude JVM targets from using this library or we can provide a custom auth screen for Desktop with pin number or even let the user provides his own implementation for it when isBiometricAvailable returns false?

sgireddy commented 3 months ago

@omarzer0 were you able to solve this issue, I mean were you able to use moko-biometry while supporting desktop app?

Any update on this issue? tried to work around for many hours but ending up with "@Composable invocations can only happen from the context of a @Composable function" no matter what I do, either I support desktop app or biometry, not a great choice either way.

interface BiometryAuthenticator {
    suspend fun checkBiometryAuthentication(
        requestTitle: String,
        requestReason: String,
        failureButtonText: String,
        allowDeviceCredentials: Boolean
    ): Boolean
}

expect class BiometryAuthenticatorFactory() {
    @Composable
    fun createBiometryAuthenticator(): BiometryAuthenticator
}

// AndroidBiometryAuthenticatorFactory.kt (androidMain)
package domain

import androidx.compose.runtime.Composable
import dev.icerock.moko.biometry.compose.rememberBiometryAuthenticatorFactory
import dev.icerock.moko.resources.desc.Raw
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.biometry.BiometryAuthenticator as MokoBiometryAuthenticator

actual class BiometryAuthenticatorFactory {

    @Composable
    actual fun createBiometryAuthenticator(): BiometryAuthenticator {
        val factory = rememberBiometryAuthenticatorFactory()
        return MokoBiometryAuthenticatorWrapper(factory.createBiometryAuthenticator())
    }

    private class MokoBiometryAuthenticatorWrapper(
        private val authenticator: MokoBiometryAuthenticator
    ) : BiometryAuthenticator {
        override suspend fun checkBiometryAuthentication(
            requestTitle: String,
            requestReason: String,
            failureButtonText: String,
            allowDeviceCredentials: Boolean
        ): Boolean {
            return authenticator.checkBiometryAuthentication(
                requestTitle = StringDesc.Raw(requestTitle),
                requestReason = StringDesc.Raw(requestReason),
                failureButtonText = StringDesc.Raw(failureButtonText),
                allowDeviceCredentials = allowDeviceCredentials
            )
        }
    }
}

  @Composable
  fun LoginScreen() {
    val viewModel = getViewModel(
            key = "biometry-screen",
            factory = viewModelFactory {
                AuthViewModel(                  
                    biometryAuthenticatorFactory.createBiometryAuthenticator() <-- It breaks here 
                )
            }
        ) {
    Column() {
            BindBiometryAuthenticatorEffect(viewModel.biometryAuthenticator) <-- and here....
    }
sgireddy commented 3 months ago

I was able to solve this issue with this work around. https://stackoverflow.com/questions/78659556/composable-invocations-can-only-happen-from-the-context-of-a-composable-functi/78665157#78665157b