1.0.17 • Published 2 years ago

react-native-keychain-access v1.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-keychain-access

Interface is similar to AsyncStorage, aside from pin on android

Getting started

$ npm install react-native-keychain-access --save

Mostly automatic installation

$ react-native link react-native-keychain-access

iOS notes

  1. Add a NSFaceIDUsageDescription entry in your Info.plist.

  2. Please also note these three lines in Podfile in the example/ios folder:

plugin 'cocoapods-user-defined-build-types'
enable_user_defined_build_types!
pod 'KeychainAccess', :build_type => :dynamic_framework
  1. You have to install ruby and run bundle install once in the ios folder. After that, you will always do bundle exec pod install instead of pod install

Android notes

On android, we should get a 6-digit pin from user that looks like:

Usage

import {Platform} from 'react-native';
import {Keychain} from 'react-native-keychain-access';

// Must not be changed
const getService = () => {
    if (Platform.OS === 'android') {
        return 'protonsdk.secure_keys'
    } else if (Platform.OS === 'ios') {
        return 'proton.swift'
    } else {
        throw new Error('Unsupported Platform')
    }
}

const KeychainService = new Keychain(getService());

// Get 6-digit pin from user (Android only)
const params = {
    pin: Platform.OS === 'android' ? '123456' : undefined,
};

const key = 'PUB_K1_55VzHNofSRiJbQ5P9XWqNba2DXanF2KcCYby2EcRXyMxF5ki6k';
const value = 'PVT_K1_2eU3nimmx21gR8Z4zAtmkBTDtfiKz4j1Q7EWGSMJ9QurgQ8ECr';
const fakeKey = 'PUB_K1_7Zyp3EKYb6V9Q78tWdnnxyijFVkTGV4LcyBXwekP8EXqppFAKE';

const setItem = await KeychainService.setItem(key, value, params);
console.log(`setItem (${key} | ${value})`, setItem);

const getItem = await KeychainService.getItem(key, params);
console.log(`getItem (${key})`, getItem);

const hasItem = await KeychainService.hasItem(key);
console.log(`hasItem (${key})`, hasItem);

const hasItem2 = await KeychainService.hasItem(fakeKey);
console.log(`!hasItem (${fakeKey})`, hasItem2);

if (Platform.OS === 'android') {
    try {
        console.log('Wrong 6 Digit Pin (getItem) - Throws error');
        await KeychainService.getItem(key, { pin: '122200' });
    } catch (e) {
        console.error(e);
    }
}

const getAllKeys = await KeychainService.getAllKeys();
console.log('getAllKeys', getAllKeys);

const getWrongItem = await KeychainService.getItem(fakeKey, params);
console.log(`Get Wrong Item (${fakeKey}):`, getWrongItem);

const removeItem = await KeychainService.removeItem(key);
console.log(`removeItem (${key}`, removeItem);

const getRemovedItem = await KeychainService.getItem(key, params);
console.log(`Get Removed Item (${key})`, getRemovedItem);
1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago