Run npm i crypto-auth-data
to install the library.
Add script to your package.json
file:
"scripts": {
"generate-key": "node node_modules/crypto-auth-data/src/utils/generate-key.js -- --secret \"my-secret-phrase\" --output \"path/to/project/.env\""
},
Example of the .env file you will have, after running the script above
ENCRYPTION_KEY=eoEG4sJQxPHurfzgYSJ7Vmlwsk7poKXiHlq8MQxvjp4=
import { cryptoData } from 'crypto-auth-data';
const salt = new Uint8Array(16);
const key = process.env.ENCRYPTION_KEY
const decryptedKey = await cryptoData.decryptSecretKey(key, salt);
if (decryptedKey) {
const encryptedJWT = await cryptoData.encryptJWT(response.token.accessToken, decryptedKey);
}
Get encryptedJWT from localStorage and decrypt it
const accessToken = await cryptoData.decryptJWT(encryptedJWT, decryptedKey);
headers.set("Authorization", `Bearer ${accessToken}`);