enable/disable e2e encryption
This commit is contained in:
@@ -39,36 +39,42 @@ watchEffect(() => {
|
||||
activeNotesSource.value = getSource()
|
||||
})
|
||||
|
||||
const baseNotes = ref<BaseNotes>({})
|
||||
export const baseNotes = ref<BaseNotes>({})
|
||||
|
||||
const syncNotesLocal = (notes: BaseNotes) => {
|
||||
localStorage.setItem('notes', JSON.stringify(notes))
|
||||
}
|
||||
|
||||
export const syncNotesToFirebase = async (newNotes: BaseNotes, oldNotes?: BaseNotes) => {
|
||||
if (!db.value) throw Error("Database undefined, can't sync to Firebase")
|
||||
if (!user.value) throw Error("User undefined, can't sync to Firebase")
|
||||
const notes = encryptionKey.value
|
||||
? encryptNotes(baseNotes.value, encryptionKey.value)
|
||||
: baseNotes.value
|
||||
try {
|
||||
const docRef = doc(db.value, 'pages', user.value.uid)
|
||||
if (oldNotes) {
|
||||
const notesToDelete = Object.keys(oldNotes).filter((x) => !Object.keys(newNotes).includes(x))
|
||||
await Promise.all(
|
||||
notesToDelete.map((noteId: string) => {
|
||||
return updateDoc(docRef, { [noteId]: deleteField() })
|
||||
})
|
||||
)
|
||||
}
|
||||
await updateDoc(docRef, notes)
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
baseNotes,
|
||||
async (newBaseNotes, oldBaseNotes) => {
|
||||
if (!activeNotesSource.value || Object.keys(baseNotes.value).length === 0) return
|
||||
console.log()
|
||||
|
||||
if (activeNotesSource.value === 'local') {
|
||||
localStorage.setItem('notes', JSON.stringify(baseNotes.value))
|
||||
syncNotesLocal(baseNotes.value)
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
if (!db.value) throw Error("Database undefined, can't sync to Firebase")
|
||||
if (!user.value) throw Error("User undefined, can't sync to Firebase")
|
||||
const notes = encryptionKey.value
|
||||
? encryptNotes(baseNotes.value, encryptionKey.value)
|
||||
: baseNotes.value
|
||||
|
||||
const notesToDelete = Object.keys(oldBaseNotes).filter(
|
||||
(x) => !Object.keys(newBaseNotes).includes(x)
|
||||
)
|
||||
try {
|
||||
const docRef = doc(db.value, 'pages', user.value.uid)
|
||||
await Promise.all(
|
||||
notesToDelete.map((noteId: string) => {
|
||||
return updateDoc(docRef, { [noteId]: deleteField() })
|
||||
})
|
||||
)
|
||||
await updateDoc(docRef, notes)
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
}
|
||||
syncNotesToFirebase(newBaseNotes, oldBaseNotes)
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
|
||||
Reference in New Issue
Block a user