Skip to content

signalhub/tokenized-auth

Repository files navigation

Crypto auth data

Install

Run npm i crypto-auth-data to install the library.

How to use

1. Generate key and encrypt it like a secret

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=

2. Decrypt generated key

import { cryptoData } from 'crypto-auth-data';

const salt = new Uint8Array(16);
const key = process.env.ENCRYPTION_KEY
const decryptedKey = await cryptoData.decryptSecretKey(key, salt);

3. Encrypt JWT data and save it to localStorage, for example

if (decryptedKey) {
  const encryptedJWT = await cryptoData.encryptJWT(response.token.accessToken, decryptedKey);
}

4. Decrypt JWT data for every request

Get encryptedJWT from localStorage and decrypt it

const accessToken = await cryptoData.decryptJWT(encryptedJWT, decryptedKey);
headers.set("Authorization", `Bearer ${accessToken}`);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published