New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rn-secure-storage

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rn-secure-storage

Secure Storage for React Native (Android & iOS) - Keychain & Keystore

3.0.0-beta.0
beta
Source
npm
Version published
Weekly downloads
3.7K
-14.13%
Maintainers
1
Weekly downloads
 
Created
Source

RNSecureStorage

Secure Storage for React Native (Android & iOS) - Keychain & Keystore

Go to F.A.Q for more information.

Getting Started

With NPM

npm install --save rn-secure-storage

With YARN

yarn add rn-secure-storage

If you have experience with Android Cipher please help me to maintenance this library. Contact: hi@talut.dev

Version 3.0.0 - THIS VERSION HAS A LOT OF BREAKING CHANGES PLEASE USE CAREFULLY

  • Android part copied & modified from oblador/react-native-keychain and added new API's
  • IOS part rewrote with Swift and new API's.
  • All API's names changed new API's added.
  • Some of api return type changed.
  • removeAll added.
  • getAllKeys added.
  • multiSet added.
  • multiGet added.
  • multiRemove added.
  • getSupportedBiometryType added.
Version 2.0.5

You can still use v2.0.5

Usage


 // {accessible: ACCESSIBLE.WHEN_UNLOCKED} -> This is only for IOS
 import React from "react"
 import { Button, SafeAreaView, ScrollView, StatusBar } from "react-native"
 import RNSecureStorage, { ACCESSIBLE } from "rn-secure-storage"
 const App = () => {


    /**
     * Set a value from secure storage.
     */
  const setItem = () => {
    RNSecureStorage.setItem("token", "^W((nXWi~M`$Gtu<s+;$`M1SotPG^~", { accessible: ACCESSIBLE.WHEN_UNLOCKED })
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
    /**
     * Get a value from secure storage.
     */
  const getItem = () => {
    RNSecureStorage.getItem("token")
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
    /**
     * Remove a value from secure storage.
     */
  const removeItem = () => {
    RNSecureStorage.removeItem("token")
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
   /**
     * Removes whole RNSecureStorage data (It'll return unremoved keys)
     */
  const removeAll = () => {
    RNSecureStorage.clear()
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
   /**
     * Checks if a key has been set it'll return tru/false
     */
  const itemExist = () => {
    RNSecureStorage.exist("@refreshToken")
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
   /**
     * Get all setted keys from secure storage.
     */
  const getAllKeys = () => {
    RNSecureStorage.getAllKeys()
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
   /**
     * Multiple key pair set for secure storage. Will return unsetted keys.
     */
  const multiSet = () => {
    const pair_one = ["@idToken", "id_token_value"]
    const pair_two = ["@accessToken"]
    const pair_three = ["@refreshToken", "refresh_token_value"]
    RNSecureStorage.multiSet([pair_one, pair_two, pair_three], { accessible: ACCESSIBLE.WHEN_UNLOCKED })
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
 /**
     * Get multiple values from secure storage.
     */
  const multiGet = () => {
    RNSecureStorage.multiGet(["@idToken", "@accessToken", "@refreshToken"])
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
    /**
     * Remove values from secure storage (On error will return unremoved keys)
     */
  const multiRemove = () => {
    RNSecureStorage.multiRemove(["@refreshToken", "token"])
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }
   /**
     * Get supported biometry type (Will return FaceID, TouchID or undefined)
     */
  const getSupportedBiometryType = () => {
    RNSecureStorage.getSupportedBiometryType()
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.log(err)
      })
  }

  return (
      <SafeAreaView>
        <ScrollView>
          <Button title="Store Item" onPress={setItem} />
          <Button title="Get Item" onPress={getItem} />
          <Button title="Remove Item" onPress={removeItem} />
          <Button title="Remove All" onPress={removeAll} />
          <Button title="Is Item Exist?" onPress={itemExist} />
          <Button title="Get All Keys" onPress={getAllKeys} />
          <Button title="Multiple Set" onPress={multiSet} />
          <Button title="Multiple Get" onPress={multiGet} />
          <Button title="Multiple Remove" onPress={multiRemove} />
          <Button title="Get Supported Biometry Type" onPress={getSupportedBiometryType} />
        </ScrollView>
      </SafeAreaView>
  )
}

export default App

Options

KeyPlatformDescriptionDefault
accessibleiOS onlyThis indicates when a keychain item is accessible, see possible values in Keychain.ACCESSIBLE.Keychain.ACCESSIBLE.WHEN_UNLOCKED

Keychain.ACCESSIBLE enum

KeyDescription
WHEN_UNLOCKEDThe data in the keychain item can be accessed only while the device is unlocked by the user.
AFTER_FIRST_UNLOCKThe data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
ALWAYSThe data in the keychain item can always be accessed regardless of whether the device is locked.
WHEN_PASSCODE_SET_THIS_DEVICE_ONLYThe data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device. Items with this attribute never migrate to a new device.
WHEN_UNLOCKED_THIS_DEVICE_ONLYThe data in the keychain item can be accessed only while the device is unlocked by the user. Items with this attribute do not migrate to a new device.
AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLYThe data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user. Items with this attribute never migrate to a new device.
ALWAYS_THIS_DEVICE_ONLYThe data in the keychain item can always be accessed regardless of whether the device is locked. Items with this attribute never migrate to a new device.

F.A.Q

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

react-native

FAQs

Package last updated on 17 Jul 2020

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