-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
150 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const GENERATED_KEYS = 'generatedKeys'; | ||
export const SESSION_DATA_KEY = 'sessionDataKey'; | ||
export const SETUP_KEY = 'setupKey'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './navigation'; | ||
export * from './other'; | ||
export * from './encryption'; | ||
export * from './auth'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,85 @@ | ||
// import { createMutation, useQueryClient, createQuery } from '@tanstack/svelte-query'; | ||
// import { generateKeys } from '$services'; | ||
// import type { GeneratedKeys } from '$types'; | ||
// import { generatedKeys } from './query-keys'; | ||
|
||
export function appQueries() { | ||
// const queryClient = useQueryClient(); | ||
// const generateKeysMutation = createMutation({ | ||
// mutationFn: (passphrase: string) => generateKeys(passphrase), | ||
// onSuccess: (data) => { | ||
// queryClient.setQueryData([generatedKeys], data); | ||
// } | ||
// }); | ||
// const generatedKeysQuery = createQuery<GeneratedKeys>({ | ||
// queryKey: [generatedKeys] | ||
// }); | ||
// return { generateKeysMutation, generatedKeysQuery }; | ||
import { createMutation, useQueryClient, createQuery } from '@tanstack/svelte-query'; | ||
import { storageService } from '$services'; | ||
import { PasswordAndSecureDataSchema, SessionStateSchema } from '$types'; | ||
import { | ||
LOCAL, | ||
PASSWORD_WITH_DEVICE_KEY, | ||
SESSION, | ||
SESSION_DATA, | ||
SESSION_DATA_KEY, | ||
SETUP_KEY | ||
} from '$const'; | ||
import { encryptData, hashPassword } from '$helpers'; | ||
|
||
export function sessionStorageQueries() { | ||
const queryClient = useQueryClient(); | ||
const sessionQuery = createQuery({ | ||
queryKey: [SESSION_DATA_KEY], | ||
queryFn: async () => { | ||
const data = await storageService.getWithoutCallback({ | ||
key: SESSION_DATA, | ||
area: SESSION | ||
}); | ||
|
||
const validatedData = SessionStateSchema.safeParse(data); | ||
return validatedData.success ? validatedData.data : false; | ||
} | ||
}); | ||
const signInMutation = createMutation({ | ||
mutationFn: async (password: string) => { | ||
const result = await storageService.getWithoutCallback({ | ||
key: PASSWORD_WITH_DEVICE_KEY, | ||
area: LOCAL | ||
}); | ||
|
||
const validatedResult = PasswordAndSecureDataSchema.safeParse(result); | ||
|
||
if ( | ||
validatedResult.success && | ||
(await hashPassword(password)) === validatedResult.data.password | ||
) { | ||
return storageService.set({ | ||
key: SESSION_DATA, | ||
value: true, | ||
area: SESSION | ||
}); | ||
} | ||
|
||
throw new Error('Invalid password or data'); | ||
}, | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SESSION_DATA_KEY] }); | ||
} | ||
}); | ||
const setupQuery = createQuery({ | ||
queryKey: [SETUP_KEY], | ||
queryFn: async () => { | ||
const data = await storageService.getWithoutCallback({ | ||
key: PASSWORD_WITH_DEVICE_KEY, | ||
area: LOCAL | ||
}); | ||
|
||
const validatedData = PasswordAndSecureDataSchema.safeParse(data); | ||
return validatedData.success; | ||
} | ||
}); | ||
const passwordWithDeviceKeyMutation = createMutation({ | ||
mutationFn: async (mutationData: { password: string; secretData: Uint8Array }) => { | ||
const hash = await hashPassword(mutationData.password); | ||
storageService.set({ | ||
key: PASSWORD_WITH_DEVICE_KEY, | ||
value: { | ||
password: hash, | ||
secureData: await encryptData(mutationData.secretData, hash) | ||
}, | ||
area: LOCAL | ||
}); | ||
}, | ||
|
||
onSuccess: () => { | ||
queryClient.invalidateQueries({ queryKey: [SETUP_KEY] }); | ||
} | ||
}); | ||
|
||
return { setupQuery, passwordWithDeviceKeyMutation, sessionQuery, signInMutation }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
export * from './passphrase'; | ||
export * from './password-exist'; | ||
export * from './keys-store'; | ||
export * from './session'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.