Skip to content

mahlon-gumbs/react-native-touch-id

 
 

Repository files navigation

React Native Touch ID

npm version npm downloads Code Climate

React Native Touch ID is a React Native library for authenticating users with Touch ID.

react-native-touch-id

Documentation

Install

npm i --save react-native-touch-id

Usage

Linking the Library

In order to use Touch ID, you must first link the library to your project. There's excellent documentation on how to do this in the React Native Docs.

Requesting Touch ID Authentication

Once you've linked the library, you'll want to make it available to your app by requiring it:

var TouchID = require('react-native-touch-id');

Requesting Touch ID authentication is as simple as calling:

TouchID.authenticate('to demo this react-native component')
  .then(success => {
    // Success code
  })
  .catch(error => {
    // Failure code
  });

Example

Using Touch ID in your app will usually look like this:

var TouchID = require('react-native-touch-id');

var YourComponent = React.createClass({
  _pressHandler() {
    TouchID.authenticate('to demo this react-native component')
      .then(success => {
        AlertIOS.alert('Authenticated Successfully');
      })
      .catch(error => {
        AlertIOS.alert('Authentication Failed');
      });
  },

  render() {
    return (
      <View>
        ...
        <TouchableHighlight onPress={this._pressHandler}>
          <Text>
            Authenticate with Touch ID
          </Text>
        </TouchableHighlight>
      </View>
    );
  }
});

Fallback

When Touch ID isn't available on a device, you should fallback to using something like Passcode Auth to authenticate users.

import TouchID from 'react-native-touch-id';
import PasscodeAuth from 'react-native-passcode-auth';
const reason = 'to demo this react-native component';

TouchID.authenticate(reason)
  .then(success => {
    // Success code
  })
  .catch(fallbackAuth);
  
function fallbackAuth(reason) {
  return PasscodeAuth.authenticate(reason)
  .then(success => {
    // Success code
  })
  .catch(error => {
    // Failure code
  });
}

Methods

authenticate(reason)

Attempts to authenticate with Touch ID. Returns a Promise object.

Arguments

  • reason - An optional String that provides a clear reason for requesting authentication.

Examples

TouchID.authenticate('to demo this react-native component')
  .then(success => {
    // Success code
    console.log('User authenticated with TouchID');
  })
  .catch(error => {
    // Failure code
    console.log(error);
  });

isSupported()

Verify's that Touch ID is supported. Returns a Promise object.

Examples

TouchID.isSupported()
  .then(supported => {
    // Success code
    console.log('TouchID is supported.');
  })
  .catch(error => {
    // Failure code
    console.log(error);
  });

Errors

There are various reasons why authenticating with Touch ID may fail. Whenever calling Touch ID authentication fails, TouchID.authenticate will return an error code representing the reason.

Below is a list of error codes that can be returned:

Code Description
LAErrorAuthenticationFailed Authentication was not successful because the user failed to provide valid credentials.
LAErrorUserCancel Authentication was canceled by the user—for example, the user tapped Cancel in the dialog.
LAErrorUserFallback Authentication was canceled because the user tapped the fallback button (Enter Password).
LAErrorSystemCancel Authentication was canceled by system—for example, if another application came to foreground while the authentication dialog was up.
LAErrorPasscodeNotSet Authentication could not start because the passcode is not set on the device.
LAErrorTouchIDNotAvailable Authentication could not start because Touch ID is not available on the device
LAErrorTouchIDNotEnrolled Authentication could not start because Touch ID has no enrolled fingers.
RCTTouchIDUnknownError Could not authenticate for an unknown reason.
RCTTouchIDNotSupported Device does not support Touch ID.

More information on errors can be found in Apple's Documentation.

License

Copyright (c) 2015, Naoufal Kadhom

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

About

React Native authentication with the native Touch ID popup.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 51.2%
  • JavaScript 48.8%