Git Product home page Git Product logo

moko-biometry's Introduction

moko-biometry
GitHub license Download kotlin-version

Mobile Kotlin biometry

This is a Kotlin Multiplatform library that provides authentication by FaceId and TouchId(Fingerprint)

Table of Contents

Features

  • Biometric user authentication - allows you to use familiar user authentication methods from business logic
  • Compose Multiplatform support (partly, mobile platforms: Android, iOS)

Requirements

  • Gradle version 6.8+
  • Android API 16+
  • iOS version 11.0+

Installation

root build.gradle

allprojects {
    repositories {
        mavenCentral()
    }
}

project build.gradle

dependencies {
    commonMainApi("dev.icerock.moko:biometry:0.4.0")

    // Compose Multiplatform
    commonMainApi("dev.icerock.moko:biometry-compose:0.4.0")

    // Jetpack Compose (only for android, if you don't use multiplatform)
    implementation("dev.icerock.moko:biometry-compose:0.4.0")
}

Usage

common

In commonMain we should create ViewModel like:

class SampleViewModel(
    val biometryAuthenticator: BiometryAuthenticator
) : ViewModel() {

    fun tryToAuth() = viewModelScope.launch {
        try {
            val isSuccess = biometryAuthenticator.checkBiometryAuthentication(
                requestTitle = "Biometry".desc(),
                requestReason = "Just for test".desc(),
                failureButtonText = "Oops".desc(),
                allowDeviceCredentials = false // true - if biometric permission is not granted user can authorise by device creds
            )

            if (isSuccess) {
                // Do something onSuccess
            }
        } catch (throwable: Throwable) {
            // Do something onFailed
        }
    }
}

After create ViewModel, let's integrate on platform.

Android

class MainActivity : AppCompatActivity() {

    private lateinit var viewModel: SampleViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create viewModel from common code.
        viewModel = getViewModel {
            SampleViewModel(
                // Pass platform implementation of the Biometry Authenticator
                // to a common code
                biometryAuthenticator = BiometryAuthenticator(
                    applicationContext = applicationContext
                )
            )
        }

        // Binds the Biometry Authenticator to the view lifecycle
        viewModel.biometryAuthenticator.bind(
            lifecycle = this@MainActivity.lifecycle,
            fragmentManager = supportFragmentManager
        )
    }
}

Compose:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setContent {
            val biometryFactory: BiometryAuthenticatorFactory = rememberBiometryAuthenticatorFactory()

            // Create viewModel from common code
            val viewModel = getViewModel {
                SampleViewModel(
                    // Pass platform implementation of the Biometry Authenticator
                    // to a common code
                    biometryAuthenticator = biometryFactory.createBiometryAuthenticator()
                )
            }

            // Binds the Biometry Authenticator to the view lifecycle
            BindBiometryAuthenticatorEffect(viewModel.biometryAuthenticator)

            // Same screen content here
        }
    }
}

iOS:

class SampleViewController: UIViewController {
    
    private var viewModel: SampleViewModel!
        
    override func viewDidLoad() {
        super.viewDidLoad()

        self.viewModel = SampleViewModel(
            biometryAuthenticator: BiometryBiometryAuthenticator(),
        )
    }
    
    @IBAction private func loginAction() {
        self.viewModel.tryToAuth()
    }
}

Additionally, you need add NSFaceIDUsageDescription key in Info.plist of your project:

<key>NSFaceIDUsageDescription</key>
<string>$(PRODUCT_NAME) Authentication with TouchId or FaceID</string>

Compose Multiplatform:

@Composable
fun BiometryScreen() {
    val biometryFactory: BiometryAuthenticatorFactory = rememberBiometryAuthenticatorFactory()
    
    BiometryScreen(
        viewModel = getViewModel(
            key = "biometry-screen",
            factory = viewModelFactory {
                BiometryViewModel(
                    biometryAuthenticator = biometryAuthenticatorFactory.createBiometryAuthenticator()
                )
            }
        )
    )
}

@Composable
private fun BiometryScreen(
    viewModel: BiometryViewModel
) = NavigationScreen(title = "moko-biometry") { paddingValues ->
    BindBiometryAuthenticatorEffect(viewModel.biometryAuthenticator)

    val text: String by viewModel.result.collectAsState()

    Column(
        modifier = Modifier.fillMaxSize().padding(paddingValues),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = text)

        Button(onClick = viewModel::onButtonClick) {
            Text(text = "Click on me")
        }
    }
}

Samples

Please see more examples in the sample directory.

Set Up Locally

Contributing

All development (both new features and bug fixes) is performed in the develop branch. This way master always contains the sources of the most recently released version. Please send PRs with bug fixes to the develop branch. Documentation fixes in the markdown files are an exception to this rule. They are updated directly in master.

The develop branch is pushed to master on release.

For more details on contributing please see the contributing guide.

License

Copyright 2021 IceRock MAG Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

moko-biometry's People

Contributors

alex009 avatar dorofeev avatar tunetab avatar anton6tak avatar exndy avatar tetraquark avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.