If you really want to secure your local storage data, use the npm module crypto-js.
Step 1.
npm install crypto-js --save
Step 2. Create a file and call it util.js and paste the following code.
import CryptoJS from 'crypto-js';
export const encryptData = (data, salt) =>
CryptoJS.AES.encrypt(JSON.stringify(data), salt).toString();
export const decryptData = (ciphertext, salt) => {
const bytes = CryptoJS.AES.decrypt(ciphertext, salt);
try {
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
}catch(err){
return null;
}
}
Step 3. Encrypt the data using encryptData():
import {encryptData} from './utils';
const originalData={
name:'IDKBlogs'
}
const salt = process.env.SALT || '6d090796-ecdf-11ea-adc1-0242ac112345';
const encryptedData = encryptData(originalData, salt);
localStorage.setItem('SOME_NON_RADABLE_KEY', encryptedData); // SOME_NON_RADABLE_KEY like 'mk' as 'myKey'
Step 4. Decrypt the data using decryptData():
import { decryptData } from './utils';
let mkLocalData = localStorage.getItem('mk');
if(!mkLocalData){
// Handle, if there is no data in localStorage, or if someone deleted the localStorage.
}
const salt = process.env.SALT || '6d090796-ecdf-11ea-adc1-0242ac112345';
const originalData = decryptData(mkLocalData, salt);
if(!originalData){
// will executes if someone altered the code in localstorage.
}
You can learn more about this here https://idkblogs.com/js/304/Secure-your-app-data-in-localStorage