A secure, lightweight wrapper for localStorage, sessionStorage, and AsyncStorage (React Native) with automatic encryption. Prevent casual inspection of your application's stored data across all platforms.
- Automatic Encryption: Data is encrypted before being stored.
- React & React Native Support: Hooks for both synchronous and asynchronous storage.
- SSR Friendly: Works perfectly with Next.js and other SSR frameworks.
- Zero External Dependencies: Lightweight and fast.
- Type Safe: Full TypeScript support.
npm install safe-session-storageUse useSafeStorage for synchronous localStorage or sessionStorage.
import { useSafeStorage } from 'safe-session-storage';
function App() {
const [theme, setTheme] = useSafeStorage('theme', 'light');
// ...
}useSafeStorage is SSR-safe. It detects the environment and returns the initial value during server-side rendering, then hydrates correctly on the client.
Use useAsyncSafeStorage with @react-native-async-storage/async-storage.
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useAsyncSafeStorage } from 'safe-session-storage';
function Profile() {
const [user, setUser, loading] = useAsyncSafeStorage('user', null, {
customStorage: AsyncStorage,
secretKey: 'my-mobile-secret'
});
if (loading) return <Text>Loading...</Text>;
// ...
}import { safeLocal, SafeStorage, AsyncSafeStorage } from 'safe-session-storage';
// Web (Sync)
safeLocal.setItem('key', 'value');
// React Native / Custom (Async)
const asyncStore = new AsyncSafeStorage({
customStorage: AsyncStorage,
secretKey: 'key'
});
await asyncStore.setItem('key', 'value');When you store data in storage engines, it is usually stored in plain text. Anyone with access to the device or a malicious script can easily read this data.
safe-session-storage obfuscates this data using XOR encryption and Base64 encoding. It works across:
- Browsers: Chrome, Firefox, Safari, Edge.
- Server: Node.js (SSR safe).
- Mobile: React Native (iOS/Android).
useSafeStorage(key, initialValue, options)safeLocal/safeSessioninstances.SafeStorageclass.
useAsyncSafeStorage(key, initialValue, options)AsyncSafeStorageclass.
Feel free to fork and improve this project! Pull requests are welcome.
This project is licensed under the Apache-2.0 License.