Giter VIP home page Giter VIP logo

expo-encrypted-storage's Introduction

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

expo-encrypted-storage's People

Contributors

skhye05 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.