Socket
Book a DemoInstallSign in
Socket

expo-encrypted-storage

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expo-encrypted-storage

Secure Storage

latest
Source
npmnpm
Version
0.1.5
Version published
Maintainers
1
Created
Source

React Native Expo Encrypted Storage

Expo wrapper to React Native Encrypted Storage.

Installation

Via yarn

$ yarn add expo-encrypted-storage

Via npm

$ npm install expo-encrypted-storage

Linking

  • React Native 0.60+

Since version 0.60, React Native supports auto linking. This means no additional step is needed on your end.

  • React Native <= 0.59
$ react-native link expo-encrypted-storage

Special note for iOS using cocoapods, run:

$ npx pod-install

Usage

This module exposes four (4) native functions to store, retrieve, remove and clear values. They can be used like so:

Import

import ExpoEncryptedStorage from "expo-encrypted-storage";

Storing a value

async function storeUserSession() {
  try {
    await ExpoEncryptedStorage.setItemAsync(
      "user_session",
      JSON.stringify({
        age: 21,
        token: "ACCESS_TOKEN",
        username: "emeraldsanto",
        languages: ["fr", "en", "de"],
      })
    );

    // Congrats! You've just stored your first value!
  } catch (error) {
    // There was an error on the native side
  }
}

Retrieving a value

async function retrieveUserSession() {
  try {
    const session = await ExpoEncryptedStorage.getItemAsync("user_session");

    if (session !== undefined) {
      // Congrats! You've just retrieved your first value!
    }
  } catch (error) {
    // There was an error on the native side
  }
}

Removing a value

async function removeUserSession() {
  try {
    await ExpoEncryptedStorage.removeItemAsync("user_session");
    // Congrats! You've just removed your first value!
  } catch (error) {
    // There was an error on the native side
  }
}

Clearing all previously saved values

async function clearStorage() {
  try {
    await ExpoEncryptedStorage.clearAsync();
    // Congrats! You've just cleared the device storage!
  } catch (error) {
    // There was an error on the native side
  }
}

Error handling

Take the removeItem example, an error can occur when trying to remove a value which does not exist, or for any other reason. This module forwards the native iOS Security framework error codes to help with debugging.

async function removeUserSession() {
  try {
    await ExpoEncryptedStorage.removeItemAsync("user_session");
  } catch (error) {
    // There was an error on the native side
    // You can find out more about this error by using the `error.code` property
    console.log(error.code); // ex: -25300 (errSecItemNotFound)
  }
}

Limitations

There seems to be some confusion around the maximum size of items that can be stored, especially on iOS. According to this StackOverflow question, the actual Keychain limit is much lower than what it should theoretically be. This does not affect Android as the EncryptedSharedPreferences API relies on the phone's storage, via XML files.

License

MIT

Keywords

react-native

FAQs

Package last updated on 06 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts