Home

Awesome

React Native Passcode Auth

npm version npm downloads

React Native Passcode Auth is a React Native library for authenticating users with iOS Passcode. It is an excellent fallback for when Touch ID is not available.

react-native-passcode-auth

Documentation

Install

React Native Passcode Auth requires iOS 9.0 or later.

npm i --save react-native-passcode-auth

Usage

Linking the Library

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

Requesting Passcode Authentication

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

import PasscodeAuth from 'react-native-passcode-auth';

Requesting Passcode authentication is as simple as calling:

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

Example

Using PasscodeAuth in your app will usually look like this:

import React, { Component, AlertIOS, TouchableHighlight, View, Text } from 'react-native';
import PasscodeAuth form 'react-native-passcode-auth';

class YourComponent extends Component {
  _pressHandler() {
    PasscodeAuth.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 Passcode
          </Text>
        </TouchableHighlight>
      </View>
    );
  }
});

Methods

authenticate(reason)

Attempts to authenticate with Passcode. Returns a Promise object.

Arguments

Examples

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

isSupported()

Verify's that Passcode Auth is supported and that Passcode is set. Returns a Promise object.

Examples

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

Errors

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

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

CodeDescription
LAErrorAuthenticationFailedAuthentication was not successful because the user failed to provide valid credentials.
LAErrorUserCancelAuthentication was canceled by the user—for example, the user tapped Cancel in the dialog.
LAErrorUserFallbackAuthentication was canceled because the user tapped the fallback button (Enter Password).
LAErrorSystemCancelAuthentication was canceled by system—for example, if another application came to foreground while the authentication dialog was up.
LAErrorPasscodeNotSetAuthentication could not start because the passcode is not set on the device.
PasscodeAuthNotSetAuthentication could not start because the passcode is not set on the device.
PasscodeAuthNotSupportedDevice does not support Passcode Auth.

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.