react-native-fast-secure-storage
is a fast, secure, and extensible key-value storage solution for React Native applications.
This library provides a simple API to securely store sensitive data, leveraging the best practices for data encryption and performance.
- Fast and efficient key-value storage
- Secure data encryption
- Simple asynchronous API
- Cross-platform support (iOS and Android)
- Supports both strings and binary data
npm install react-native-fast-secure-storage
or
yarn add react-native-fast-secure-storage
- setItem
- getItem
- clearStorage
- setItems
- getAllKeys
- getAllItems
- removeItem
- clearStorage
- hasItem
- getItemSync
- setItemSync
- hasItemSync
- removeItemSync
Stores an item in the secure storage.
- key: The key of the item.
- value: The value of the item.
- accessible: (Optional) The accessibility level of the item. Default is ACCESSIBLE.WHEN_UNLOCKED.
import SecureStorage, { ACCESSIBLE } from "react-native-fast-secure-storage";
const result = await SecureStorage.setItem("key1", "value1", ACCESSIBLE.WHEN_UNLOCKED)
Retrieves an item from the secure storage.
- key: The key of the item to retrieve.
import SecureStorage from "react-native-fast-secure-storage";
const result = await SecureStorage.getItem("key1")
console.log(result); // output: value1
This method throws an error if there is no stored value for this key or the value is empty.
Clears all items from the secure storage.
import SecureStorage from "react-native-fast-secure-storage";
await SecureStorage.clearStorage()
Stores multiple items in the secure storage.
- items: An array of items to set.
import SecureStorage, { ACCESSIBLE } from "react-native-fast-secure-storage";
const testItems = [
{
key: "key1",
value: "value1",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
{
key: "key2",
value: "value2",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
];
await SecureStorage.setItems(testItems);
Retrieves all keys from the secure storage.
import SecureStorage, { ACCESSIBLE } from "react-native-fast-secure-storage";
const testItems = [
{
key: "key1",
value: "value1",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
{
key: "key2",
value: "value2",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
];
await SecureStorage.setItems(testItems);
const keys = SecureStorage.getAllKeys();
console.log(keys); // output: ["ke1", "key2"]
Retrieves all items from the secure storage.
import SecureStorage, { ACCESSIBLE } from "react-native-fast-secure-storage";
const testItems = [
{
key: "key1",
value: "value1",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
{
key: "key2",
value: "value2",
accessibleValue: ACCESSIBLE.WHEN_UNLOCKED
},
];
await SecureStorage.setItems(testItems);
const secureStorageItems = SecureStorage.getAllKeys();
console.log(secureStorageItems); // output: [{ key: "key1", value: "value1" }, {key: "key2", value: "value2" }]
Removes an item from the secure storage.
- key: The key of the item to remove.
import SecureStorage from "react-native-fast-secure-storage";
await SecureStorage.setItem("key1", "value1");
await SecureStorage.deleteItem("key1");
try {
await SecureStorage.getItem("key1")
} cath(e) {
console.log(e) // output: value does not exist
}
- key: The key of the item to check.
import SecureStorage from "react-native-fast-secure-storage";
await SecureStorage.setItem("key1", "value1");
const result = await SecureStorage.hasItem("key1");
console.log(result) // output: true
Synchronously retrieves an item from the secure storage.
- key: The key of the item to retrieve.
import SecureStorage from 'react-native-fast-secure-storage';
const value = secureStorage.getItemSync('key1');
console.log(value); // Output: 'value1'
Stores an item in the secure storage synchronously.
- key: The key of the item.
- value: The value of the item.
- accessible: (Optional) The accessibility level of the item. Default is ACCESSIBLE.WHEN_UNLOCKED.
const setResult = secureStorage.setItemSync('key1', 'value1');
console.log(setResult); // Output: true
Checks if an item exists in the secure storage synchronously.
- key: The key of the item to check.
const setResult = secureStorage.setItemSync('key1', 'value1');
const exists = secureStorage.hasItemSync('key1');
console.log(exists); // Output: true
Removes an item from the secure storage synchronously.
- key: The key of the item to remove.
const setResult = secureStorage.setItemSync('key1', 'value1');
const removeResult = secureStorage.removeItemSync('key1');
This library includes a built in mock for Jest. To use it, add the following code to the jest setup file:
jest.mock('react-native-fast-secure-storage', () => require("react-native-fast-secure-storage/jest"));
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library