Convert Figma logo to code with AI

oblador logoreact-native-keychain

:key: Keychain Access for React Native

3,347
527
3,347
159

Top Related Projects

💬 The most complete chat UI for React Native

An asynchronous, persistent, key-value storage system for React Native.

Device Information for React Native iOS and Android

React Native Network Info API for Android & iOS

Geolocation APIs for React Native

Quick Overview

React Native Keychain is a library that provides keychain/keystore access for React Native applications. It allows secure storage of sensitive information like passwords, tokens, and other credentials on both iOS and Android platforms, leveraging the native security features of each operating system.

Pros

  • Cross-platform support for iOS and Android
  • Easy-to-use API for storing and retrieving sensitive data
  • Utilizes native keychain/keystore for enhanced security
  • Supports biometric authentication (TouchID, FaceID, and Android fingerprint)

Cons

  • Limited support for complex data structures (primarily designed for key-value pairs)
  • Potential issues with data migration when upgrading the app
  • May require additional configuration for certain Android versions
  • Limited control over the underlying security mechanisms

Code Examples

  1. Storing credentials:
import * as Keychain from 'react-native-keychain';

// Store username and password
await Keychain.setGenericPassword('username', 'password');
  1. Retrieving credentials:
import * as Keychain from 'react-native-keychain';

// Retrieve username and password
const credentials = await Keychain.getGenericPassword();
if (credentials) {
  console.log('Credentials successfully loaded', credentials);
} else {
  console.log('No credentials stored');
}
  1. Storing and retrieving custom data:
import * as Keychain from 'react-native-keychain';

// Store custom data
await Keychain.setInternetCredentials('my-api', 'token', 'secret-api-key');

// Retrieve custom data
const credentials = await Keychain.getInternetCredentials('my-api');
if (credentials) {
  console.log('API credentials:', credentials);
}
  1. Using biometric authentication:
import * as Keychain from 'react-native-keychain';

// Store credentials with biometric protection
await Keychain.setGenericPassword('username', 'password', {
  accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_ANY,
  accessible: Keychain.ACCESSIBLE.WHEN_UNLOCKED,
});

// Retrieve credentials with biometric authentication
const credentials = await Keychain.getGenericPassword({
  authenticationPrompt: 'Authenticate to access your account',
});

Getting Started

  1. Install the library:

    npm install react-native-keychain
    
  2. For iOS, run:

    cd ios && pod install
    
  3. Import and use in your React Native app:

    import * as Keychain from 'react-native-keychain';
    
    // Store credentials
    await Keychain.setGenericPassword('username', 'password');
    
    // Retrieve credentials
    const credentials = await Keychain.getGenericPassword();
    
  4. For Android, ensure you have the correct minSdkVersion in your android/build.gradle file:

    buildscript {
      ext {
        minSdkVersion = 21
      }
    }
    

Competitor Comparisons

💬 The most complete chat UI for React Native

Pros of react-native-gifted-chat

  • Provides a full-featured chat UI component out of the box
  • Supports customization of chat bubbles, input toolbar, and other elements
  • Includes features like message loading, typing indicators, and avatar support

Cons of react-native-gifted-chat

  • Larger package size due to comprehensive UI components
  • May require more setup and configuration for basic chat functionality
  • Less focused on security compared to react-native-keychain

Code Comparison

react-native-gifted-chat:

import { GiftedChat } from 'react-native-gifted-chat'

<GiftedChat
  messages={this.state.messages}
  onSend={messages => this.onSend(messages)}
  user={{
    _id: 1,
  }}
/>

react-native-keychain:

import * as Keychain from 'react-native-keychain';

await Keychain.setGenericPassword('username', 'password');
const credentials = await Keychain.getGenericPassword();

The code snippets demonstrate the primary use cases for each library. react-native-gifted-chat focuses on rendering a chat interface, while react-native-keychain provides secure storage for sensitive data. The choice between these libraries depends on the specific requirements of your React Native application, whether you need a full chat UI or secure credential storage.

An asynchronous, persistent, key-value storage system for React Native.

Pros of async-storage

  • Simple and easy-to-use API for general-purpose data storage
  • Supports storing various data types (strings, objects, arrays)
  • Widely adopted and maintained by the React Native community

Cons of async-storage

  • Not designed for secure storage of sensitive information
  • Limited to key-value pair storage, lacking advanced querying capabilities

Code Comparison

react-native-keychain:

import * as Keychain from 'react-native-keychain';

await Keychain.setGenericPassword('username', 'password');
const credentials = await Keychain.getGenericPassword();

async-storage:

import AsyncStorage from '@react-native-async-storage/async-storage';

await AsyncStorage.setItem('user', JSON.stringify({ username: 'user', password: 'pass' }));
const user = JSON.parse(await AsyncStorage.getItem('user'));

Summary

react-native-keychain is specifically designed for secure storage of sensitive information like credentials, while async-storage is a general-purpose storage solution for React Native applications. react-native-keychain provides better security features and is more suitable for storing sensitive data, whereas async-storage offers a simpler API and broader data storage capabilities for non-sensitive information. The choice between the two depends on the specific requirements of your application, particularly regarding data sensitivity and storage needs.

Device Information for React Native iOS and Android

Pros of react-native-device-info

  • Provides a wide range of device information beyond security features
  • Offers cross-platform support for iOS and Android
  • Regularly updated with new features and device compatibility

Cons of react-native-device-info

  • Focuses on device information rather than secure storage
  • May require additional permissions, potentially raising privacy concerns
  • Larger package size due to broader feature set

Code Comparison

react-native-device-info:

import DeviceInfo from 'react-native-device-info';

const deviceId = DeviceInfo.getUniqueId();
const manufacturer = DeviceInfo.getManufacturer();
const model = DeviceInfo.getModel();

react-native-keychain:

import * as Keychain from 'react-native-keychain';

await Keychain.setGenericPassword('username', 'password');
const credentials = await Keychain.getGenericPassword();
await Keychain.resetGenericPassword();

react-native-device-info provides comprehensive device information, while react-native-keychain focuses on secure credential storage. The former is ideal for gathering device-specific data, while the latter is better suited for handling sensitive information securely. Choose based on your specific requirements: device info or secure storage.

React Native Network Info API for Android & iOS

Pros of react-native-netinfo

  • Focused on network connectivity information, providing detailed network status and type
  • Offers real-time network change detection and event listeners
  • Supports both iOS and Android platforms with consistent API

Cons of react-native-netinfo

  • Limited to network-related functionality, not suitable for secure data storage
  • May require additional setup for certain network types (e.g., cellular)
  • Potential battery drain due to constant network monitoring

Code Comparison

react-native-netinfo:

import NetInfo from "@react-native-community/netinfo";

NetInfo.fetch().then(state => {
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
});

react-native-keychain:

import * as Keychain from 'react-native-keychain';

await Keychain.setGenericPassword('username', 'password');
const credentials = await Keychain.getGenericPassword();
console.log('Credentials successfully loaded', credentials);

Summary

react-native-netinfo focuses on providing network connectivity information and status updates, while react-native-keychain is designed for secure credential storage. The former is ideal for applications requiring real-time network monitoring, while the latter is better suited for managing sensitive user data. Choose based on your specific project requirements: network monitoring (react-native-netinfo) or secure storage (react-native-keychain).

Geolocation APIs for React Native

Pros of react-native-geolocation

  • Focused specifically on geolocation functionality
  • Provides more detailed location information and options
  • Supports both iOS and Android platforms

Cons of react-native-geolocation

  • Limited to geolocation features only
  • May require additional permissions and setup for location services
  • Potentially higher battery consumption due to GPS usage

Code Comparison

react-native-geolocation:

import Geolocation from 'react-native-geolocation-service';

Geolocation.getCurrentPosition(
  (position) => {
    console.log(position);
  },
  (error) => {
    console.log(error.code, error.message);
  },
  { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);

react-native-keychain:

import * as Keychain from 'react-native-keychain';

await Keychain.setGenericPassword('username', 'password');
const credentials = await Keychain.getGenericPassword();
console.log('Credentials successfully loaded', credentials);

Summary

react-native-geolocation is specialized for location services, offering detailed geolocation features for both iOS and Android. It provides more options for location tracking but may require additional setup and permissions. In contrast, react-native-keychain focuses on secure storage of sensitive information, which is a different use case altogether. The choice between these libraries depends on the specific requirements of your project: geolocation functionality or secure data storage.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-native-keychain

Tests npm npm

This library provides access to the Keychain (iOS) and Keystore (Android) for securely storing credentials like passwords, tokens, or other sensitive information in React Native apps.

Installation

  1. Run yarn add react-native-keychain
  2. Run pod install in ios/ directory to install iOS dependencies.
  3. If you want to support FaceID, add a NSFaceIDUsageDescription entry in your Info.plist.
  4. Re-build your Android and iOS projects.

Documentation

Please refer to the documentation website on https://oblador.github.io/react-native-keychain

Changelog

Check the GitHub Releases page.

Maintainers


Joel Arvidsson

Author

Dorian Mazur

Maintainer

Vojtech Novak

Maintainer

Pelle Stenild Coltau

Maintainer

Oleksandr Kucherenko

Contributor

Used By

This library is used by several projects, including:

License

MIT © Joel Arvidsson 2016-2020

NPM DownloadsLast 30 Days