React Native Keychain Access

10.0.0 · active · verified Sun Apr 19

react-native-keychain is a library providing secure access to platform-native credential storage on iOS (Keychain) and Android (Keystore). It enables React Native applications to securely store sensitive data such as passwords, authentication tokens, and other user credentials, protecting them from unauthorized access. The current stable version is 10.0.0, released in late 2024 / early 2025. The project has a consistent release cadence with multiple patch and minor versions preceding major updates, indicating active maintenance and feature development. Key differentiators include robust support for biometric authentication, integration with Android's Jetpack DataStore for enhanced security and performance, and optional iCloud synchronization for iOS keychain items. It explicitly supports modern React Native versions and provides TypeScript definitions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to store, retrieve, and reset generic credentials (username/password) using `react-native-keychain` with specific access control and security levels.

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

async function storeAndRetrieveCredentials(username: string, password_input: string) {
  try {
    // Store the credentials
    await Keychain.setGenericPassword(username, password_input, {
      service: 'myAppService',
      accessControl: Keychain.ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY,
      securityLevel: Keychain.SECURITY_LEVEL.SECURE_SOFTWARE
    });
    console.log('Credentials successfully stored!');

    // Retrieve the credentials
    const credentials = await Keychain.getGenericPassword({
      service: 'myAppService'
    });

    if (credentials) {
      console.log('Credentials successfully loaded for user ' + credentials.username);
      console.log('Password is ' + credentials.password);
      // In a real app, you would use these credentials, not log the password directly.
    } else {
      console.log('No credentials stored.');
    }

    // Example of resetting credentials
    await Keychain.resetGenericPassword({ service: 'myAppService' });
    console.log('Credentials successfully reset!');

  } catch (error) {
    console.error('Keychain operation failed', error);
  }
}

// Example usage:
// Use environment variables or secure inputs in a real application
const myUsername = process.env.KEYCHAIN_USERNAME ?? 'testuser';
const myPassword = process.env.KEYCHAIN_PASSWORD ?? 'supersecretpassword';

storeAndRetrieveCredentials(myUsername, myPassword);

view raw JSON →