Coder Social home page Coder Social logo

srgtcauliflour / biometricauthentication Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rushisangani/biometricauthentication

0.0 0.0 0.0 296 KB

Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication.

License: MIT License

Ruby 3.41% Swift 94.03% Objective-C 2.56%

biometricauthentication's Introduction

BiometricAuthentication

Use Apple FaceID or TouchID authentication in your app using BiometricAuthentication. It's very simple and easy to use that handles Touch ID and Face ID authentication based on the device.

Note: - Face ID authentication requires user's persmission to be add in info.plist.

<key>NSFaceIDUsageDescription</key>
<string>This app requires Face ID permission to authenticate using Face recognition.</string>

Alt text Alt text Alt text

Features

  • Works with Apple Face ID (iPhone X) and other Touch ID having devices.
  • Predefined error handling when recognition fails.
  • Automatic authentication with device passcode on multiple failed attempts.

Requirements

  • iOS 8.0+
  • Xcode 8+
  • Swift 3.0+

Installation

CocoaPods

pod 'BiometricAuthentication'

Carthage

github "rushisangani/BiometricAuthentication"

Usage

Authenticate with biometric

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "", success: {
    // authentication success

}) { (error) in

    // error
    print(error.message())
}
  • When reason specified as empty - default will be used based on the device. Ex. for iPhone X - "Confirm your face to authenticate.", For other devices - "Confirm your fingerprint to authenticate."

Can Authenticate with biometric

  • Alternatively you can check before authentication by following. This will check that if device supports Touch ID or Face ID authentication and your app can use that as of now.
if BioMetricAuthenticator.canAuthenticate() {

    BioMetricAuthenticator.authenticateWithBioMetrics(reason: "", success: {
        // authentication success

    }) { (error) in

        // error
        print(error.message())
    }
}

Check for Face ID

  • Check if device supports face recognition or not.
if BioMetricAuthenticator.shared.faceIDAvailable() {
    // device supports face id recognition.
}

Fallback Reason

  • Fallback reason title will be shown when first authentication attempt is failed.
  • You can give alternate authentication options like enter 'username & password' when user clicks on fallback button.
  • Default reason is empty, which will hide fallback button.
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "Biometric Authentication", fallbackTitle: "Enter Credentails", success: {

    // authentication successful

}) { (error) in

    // show alternatives on fallback button clicked
    // for ex. - enter username/email and password
    if error == .fallback {
    
    }
}

BiometryLockedout

  • When biometry authentication is locked out after multiple failed attempts. You can unlock it by passcode authentication.
  • Provide your own passcode authentication reason here, default will be used if not provided.
BioMetricAuthenticator.authenticateWithPasscode(reason: "", success: {
    // passcode authentication success

}) { (error) in
    print(error.message())
}

Error Handling

  • There are various cases when biometric authentication can be failed.
  1. fallback
    • Called when user clicks on provided fallback button.
  2. biometryNotEnrolled
    • Called when no fingerprints or face is registered with the device.
    • You can show message to register a new face or fingerprint here.
    • Default message will be shown if not provided.
  3. canceledByUser
    • Called when authentication canceled by user.
  4. canceledBySystem
    • Called when authentication canceled by system when app goes into background or any other reason.
  5. passcodeNotSet
    • Called when device passcode is not set and trying to use biometry authentication.
    • We can ask user to set device passcode here by opening Settings Application.
  6. failed
    • Called when multiple failed attempts made by user.
    • You can show error message to user here.
    • Default message can be shown if not provided.
  7. biometryLockedout
    • Called when more than 5 failed attempts made using biometric authentication. This will locked your biometry system.
    • You'll restrict user when this error is occured.
    • Aleternatively you can ask user to enter device passcode to unlock biometry.
  8. biometryNotAvailable
    • Called when device does not support Face ID or Touch ID authentication.

Example

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "", success: {

    // authentication successful

}, failure: { [weak self] (error) in

    // do nothing on canceled
    if error == .canceledByUser || error == .canceledBySystem {
        return
    }
    
    // device does not support biometric (face id or touch id) authentication
    else if error == .biometryNotAvailable {
        self?.showErrorAlert(message: error.message())
    }

    // show alternatives on fallback button clicked
    else if error == .fallback {

        // here we're entering username and password
        self?.txtUsername.becomeFirstResponder()
    }

    // No biometry enrolled in this device, ask user to register fingerprint or face
    else if error == .biometryNotEnrolled {
        self?.showGotoSettingsAlert(message: error.message())
    }

    // Biometry is locked out now, because there were too many failed attempts.
    // Need to enter device passcode to unlock.
    else if error == .biometryLockedout {
        // show passcode authentication
    }

    // show error on authentication failed
    else {
        self?.showErrorAlert(message: error.message())
    }
})

See Example for more details.

License

BiometricAuthentication is released under the MIT license. See LICENSE for details.

biometricauthentication's People

Contributors

evertoncunha avatar rushisangani avatar dimitryvin avatar irshadpc 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.