I've been trying to encrypt my localstorage data, and although it successfully encrypts, I'm encountering an error.
Here's the code snippet (./src/utils/secureLocalStorage.js):
import SecureStorage from 'secure-web-storage'
import CryptoJS from 'crypto-js'
const SECRET_KEY = process.env.REACT_APP_SECRET_KEY
let secureLocalStorage = null
try {
secureLocalStorage = new SecureStorage(localStorage, {
hash: function hash(key) {
key = CryptoJS.SHA256(key, SECRET_KEY).toString()
return key
},
encrypt: function encrypt(data) {
data = CryptoJS.AES.encrypt(data, SECRET_KEY).toString()
return data
},
decrypt: function decrypt(data) {
try {
data = CryptoJS.AES.decrypt(data, SECRET_KEY).toString(
CryptoJS.enc.Utf8
)
return data
} catch (error) {
console.error('Error decrypting data:', error)
return null
}
},
})
} catch (error) {
console.error('localStorage is not available:', error)
}
export default secureLocalStorage
The error message appears in my terminal as shown below: