Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from emeraldsanto/dev
Browse files Browse the repository at this point in the history
Version 2.4.4
  • Loading branch information
emeraldsanto authored Jun 30, 2020
2 parents 7b3feec + 6652484 commit 6ae0e78
Show file tree
Hide file tree
Showing 75 changed files with 10,474 additions and 20 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ buck-out/
# JEST
coverage/

# TYPESCRIPT
dist/*
*.prefs
# MISC
*.prefs
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ $ react-native link react-native-encrypted-storage
Special note for iOS using `cocoapods`, run:

```bash
$ cd ios && pod install && cd ..
$ npx pod-install
```

## Usage

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

### Import

Expand All @@ -55,10 +55,10 @@ async function storeUserSession() {
await EncryptedStorage.setItem(
"user_session",
JSON.stringify({
username : "emeraldsanto",
age : 21,
languages : ["fr", "en", "de"],
token : "ACCESS_TOKEN"
token : "ACCESS_TOKEN",
username : "emeraldsanto",
languages : ["fr", "en", "de"]
})
);

Expand Down Expand Up @@ -98,6 +98,35 @@ async function removeUserSession() {
}
```

### Clearing all previously saved values

```js
async function clearStorage() {
try {
await EncryptedStorage.clear();
// 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.

```js
async function removeUserSession() {
try {
await EncryptedStorage.removeItem("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)
}
}
```

## Note regarding `Keychain` persistence

You'll notice that the iOS `Keychain` is not cleared when your app is uninstalled, this is the expected behaviour. However, if you do want to achieve a different behaviour, you can use the below snippet to clear the `Keychain` on the first launch of your app.
Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ android {
defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
versionCode 31
versionName "2.4.3"
versionCode 40
versionName "2.4.4"
}
lintOptions {
abortOnError false
Expand Down
48 changes: 48 additions & 0 deletions dist/EncryptedStorage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export declare type StorageErrorCallback = (error?: Error) => void;
export declare type StorageValueCallback = (error?: Error, value?: string) => void;
export default class EncryptedStorage {
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
*/
static setItem(key: string, value: string): Promise<void>;
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
* @param {Function} cb - The function to call when the operation completes.
*/
static setItem(key: string, value: string, cb: StorageErrorCallback): void;
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
*/
static getItem(key: string): Promise<string | null>;
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static getItem(key: string, cb: StorageValueCallback): void;
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
*/
static removeItem(key: string): Promise<void>;
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static removeItem(key: string, cb: StorageErrorCallback): void;
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
*/
static clear(): Promise<void>;
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {Function} cb - The function to call when the operation completes.
*/
static clear(cb: (error?: Error) => void): void;
}
38 changes: 38 additions & 0 deletions dist/EncryptedStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const { RNEncryptedStorage } = react_native_1.NativeModules;
if (!RNEncryptedStorage) {
throw new Error("RNEncryptedStorage is undefined");
}
class EncryptedStorage {
static setItem(key, value, cb) {
if (cb) {
RNEncryptedStorage.setItem(key, value).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.setItem(key, value);
}
static getItem(key, cb) {
if (cb) {
RNEncryptedStorage.getItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.getItem(key);
}
static removeItem(key, cb) {
if (cb) {
RNEncryptedStorage.removeItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.removeItem(key);
}
static clear(cb) {
if (cb) {
RNEncryptedStorage.clear().then(cb).catch(cb);
return;
}
return RNEncryptedStorage.clear();
}
}
exports.default = EncryptedStorage;
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./EncryptedStorage";
4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EncryptedStorage_1 = require("./EncryptedStorage");
exports.default = EncryptedStorage_1.default;
48 changes: 48 additions & 0 deletions dist/lib/EncryptedStorage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export declare type StorageErrorCallback = (error?: Error) => void;
export declare type StorageValueCallback = (error?: Error, value?: string) => void;
export default class EncryptedStorage {
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
*/
static setItem(key: string, value: string): Promise<void>;
/**
* Writes data to the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that will be associated to the value for later retrieval.
* @param {string} value - The data to store.
* @param {Function} cb - The function to call when the operation completes.
*/
static setItem(key: string, value: string, cb: StorageErrorCallback): void;
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
*/
static getItem(key: string): Promise<string | null>;
/**
* Retrieves data from the disk, using SharedPreferences or KeyChain, depending on the platform and returns it as the specified type.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static getItem(key: string, cb: StorageValueCallback): void;
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
*/
static removeItem(key: string): Promise<void>;
/**
* Deletes data from the disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {string} key - A string that is associated to a value.
* @param {Function} cb - The function to call when the operation completes.
*/
static removeItem(key: string, cb: StorageErrorCallback): void;
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
*/
static clear(): Promise<void>;
/**
* Clears all data from disk, using SharedPreferences or KeyChain, depending on the platform.
* @param {Function} cb - The function to call when the operation completes.
*/
static clear(cb: (error?: Error) => void): void;
}
38 changes: 38 additions & 0 deletions dist/lib/EncryptedStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
const { RNEncryptedStorage } = react_native_1.NativeModules;
if (!RNEncryptedStorage) {
throw new Error("RNEncryptedStorage is undefined");
}
class EncryptedStorage {
static setItem(key, value, cb) {
if (cb) {
RNEncryptedStorage.setItem(key, value).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.setItem(key, value);
}
static getItem(key, cb) {
if (cb) {
RNEncryptedStorage.getItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.getItem(key);
}
static removeItem(key, cb) {
if (cb) {
RNEncryptedStorage.removeItem(key).then(cb).catch(cb);
return;
}
return RNEncryptedStorage.removeItem(key);
}
static clear(cb) {
if (cb) {
RNEncryptedStorage.clear().then(cb).catch(cb);
return;
}
return RNEncryptedStorage.clear();
}
}
exports.default = EncryptedStorage;
1 change: 1 addition & 0 deletions dist/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./EncryptedStorage";
4 changes: 4 additions & 0 deletions dist/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EncryptedStorage_1 = require("./EncryptedStorage");
exports.default = EncryptedStorage_1.default;
6 changes: 6 additions & 0 deletions example/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[android]
target = Google Inc.:Google APIs:23

[maven_repositories]
central = https://repo1.maven.org/maven2
1 change: 1 addition & 0 deletions example/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
59 changes: 59 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore
!debug.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle

# CocoaPods
/ios/Pods/
1 change: 1 addition & 0 deletions example/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading

0 comments on commit 6ae0e78

Please sign in to comment.