-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryptedLocalStorage.js
More file actions
34 lines (29 loc) · 937 Bytes
/
encryptedLocalStorage.js
File metadata and controls
34 lines (29 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { curry, otherwise, andThen, pipe, partialRight } from 'ramda'
import { Either } from 'ramda-fantasy'
import { encrypt, decrypt } from './encryption'
export async function encryptToLocalStorage(key, data, password) {
const encryptOrPass = partialRight(
async (data, password) => data && password ? encrypt(data, password) : data,
[password]
)
const save = curry((key, data) => localStorage.setItem(key, data))
const saveToPredefinedKey = save(key)
return pipe(
JSON.stringify,
encryptOrPass,
andThen(saveToPredefinedKey)
)(data)
}
export async function decryptFromLocalStorage(key, password) {
const decryptOrPass = partialRight(
async (data, password) => data && password ? decrypt(data, password) : data,
[password]
)
return pipe(
key => localStorage.getItem(key),
decryptOrPass,
andThen(JSON.parse),
andThen(Either.Right),
otherwise(Either.Left)
)(key)
}