Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function flushStorage(newState: StorageState): Promise {
// $FlowFixMe Flow doesn't get that it is the same object
storageState = {...newState};
const pairsToRemove = Object.entries(storageState)
.filter(([key, value]) => !hasValue(value));
await AsyncStorage.multiRemove(pairsToRemove.map((([key]) => storageKeys[key])));
const pairsToWrite = Object.entries(storageState)
.filter(([key, value]) => value !== null && value !== undefined);
if (pairsToWrite.length === 0) {
log.debug('Storage state is empty, no actuall write has been done');
return newState;
}
const pairs = pairsToWrite
.map(([key, value]) => [storageKeys[key], hasValue(value) ? JSON.stringify(value) : value]);
await AsyncStorage.multiSet(pairs);
return storageState;
}
.then((allKeys) => {
let keysToDestroy = {}; // this is map to ensure that we do not delete a key twice
for (let i = 0; i < pathObjects.length; i++) {
this._selectMatchingKeys(pathObjects[i], allKeys, keysToDestroy);
}
return AsyncStorage.multiRemove(Object.keys(keysToDestroy))
})
}
return AsyncStorage.getItem(exprKey).then((expiry) => {
if (expiry && currentTime() >= parseInt(expiry, 10)){
var theKey = CACHE_PREFIX + key.replace(CACHE_EXPIRATION_PREFIX, '');
return AsyncStorage.multiRemove([exprKey, theKey]);
}
return Promise.resolve();
}).catch(() => {
return Promise.reject(null);
if (!!this.props.endProcessFunction) {
this.props.endProcessFunction(pinCode as string)
} else {
if (this.props.handleResult) {
this.props.handleResult(pinCode)
}
this.setState({ pinCodeStatus: PinResultStatus.initial })
this.props.changeInternalStatus(PinResultStatus.initial)
const pinAttemptsStr = await AsyncStorage.getItem(
this.props.pinAttemptsAsyncStorageName
)
let pinAttempts = pinAttemptsStr ? +pinAttemptsStr : 0
const pin = this.props.storedPin || this.keyChainResult
if (pin === pinCode) {
this.setState({ pinCodeStatus: PinResultStatus.success })
AsyncStorage.multiRemove([
this.props.pinAttemptsAsyncStorageName,
this.props.timePinLockedAsyncStorageName
])
this.props.changeInternalStatus(PinResultStatus.success)
if (!!this.props.finishProcess)
this.props.finishProcess(pinCode as string)
} else {
pinAttempts++
if (
+pinAttempts >= this.props.maxAttempts &&
!this.props.disableLockScreen
) {
await AsyncStorage.setItem(
this.props.timePinLockedAsyncStorageName,
new Date().toISOString()
)
multiRemove: (keys): Promise => {
return AsyncStorage.multiRemove(keys);
},
multiGet: (keys): Promise => {
if (parsedData == null) {
keysToDelete.push(key);
return;
}
const { queryID, variables } = JSON.parse(key.substring(CACHE_KEY.length));
if (this.isCurrent(parsedData.fetchTime) && cacheSize < CACHE_SIZE) {
this.cache.set(queryID, variables, parsedData.payload);
cacheSize++;
} else {
keysToDelete.push(key);
}
});
if (keysToDelete.length > 0) {
AsyncStorage.multiRemove(keysToDelete);
}
};
export async function clearBridgeCache() {
const keys = await AsyncStorage.getAllKeys();
await AsyncStorage.multiRemove(
keys.filter(k => k.startsWith("bridgeproxypreload")),
);
}
return AsyncStorage.getAllKeys().then((keys) => {
var theKeys = keys.filter((key) => {
return key.indexOf(CACHE_PREFIX) == 0 || key.indexOf(CACHE_EXPIRATION_PREFIX) == 0;
});
return AsyncStorage.multiRemove(theKeys);
}).catch(() => {
return Promise.reject(null);
AsyncStorage.getAllKeys((err, keys) => {
if (!err && keys && keys.length) {
const keyPrefix = 'persist:root';
const v5PersistKeys = keys.filter(key => key.indexOf(keyPrefix) === 0);
if (v5PersistKeys.length) {
AsyncStorage.multiRemove(v5PersistKeys, () => Promise.resolve());
} else {
const v4KeyPrefix = 'reduxPersist:';
const v4PersistKeys = keys.filter(
key => key.indexOf(v4KeyPrefix) === 0,
);
if (v4PersistKeys.length) {
AsyncStorage.multiRemove(v4PersistKeys, () => Promise.resolve());
}
}
}
return Promise.resolve();
});
const logout = React.useCallback(async () => {
try {
await fetchWithAuth(`/logout?apisecret=${secret}`, {
method: 'POST',
});
} catch (err) {
} finally {
await AsyncStorage.multiRemove([
STORAGE_KEYS.auth,
STORAGE_KEYS.username,
STORAGE_KEYS.secret,
]);
setAuth(null);
setUsername(null);
setSecret(null);
}
}, [fetchWithAuth, secret]);