diff --git a/src/AuthContext/use-context-value.ts b/src/AuthContext/use-context-value.ts index 34bcd6c..02af920 100644 --- a/src/AuthContext/use-context-value.ts +++ b/src/AuthContext/use-context-value.ts @@ -8,7 +8,7 @@ export interface UseContextValueData { encryptUsingMasterPassword: (content: string) => string decryptUsingMasterPassword: (content: string) => string decryptUsingPrivateKey: (message: string) => Promise - setDecryptionPassword: (password: string) => void + setDecryptionPassword: (password: string) => boolean } export default function useContextValue({ diff --git a/src/AuthContext/use-master-password.ts b/src/AuthContext/use-master-password.ts index 19e02ea..4613850 100644 --- a/src/AuthContext/use-master-password.ts +++ b/src/AuthContext/use-master-password.ts @@ -10,7 +10,7 @@ export interface UseMasterPasswordResult { decryptUsingMasterPassword: (content: string) => string decryptUsingPrivateKey: (message: string) => Promise - setDecryptionPassword: (password: string) => void + setDecryptionPassword: (password: string) => boolean logout: () => void // Use this cautiously _masterPassword: string @@ -78,6 +78,22 @@ export default function useMasterPassword( [user], ) + const updateDecryptionPassword = useCallback((password: string) => { + if (!user || !user.encryptedPassword) { + throw new Error("User not set.") + } + + try { + const masterPassword = decryptString(user.encryptedPassword, password) + JSON.parse(decryptString((user as ServerUser).encryptedNotes, masterPassword)) + setDecryptionPassword(password) + } catch { + return false; + } + + return true; + }, [user, masterPassword]) + const logout = useCallback(() => { setDecryptionPassword(null) }, []) @@ -86,8 +102,8 @@ export default function useMasterPassword( encryptUsingMasterPassword, decryptUsingMasterPassword, decryptUsingPrivateKey, - setDecryptionPassword, logout, + setDecryptionPassword: updateDecryptionPassword, _masterPassword: masterPassword!, } } diff --git a/src/AuthContext/use-user.ts b/src/AuthContext/use-user.ts index 671048d..7aff447 100644 --- a/src/AuthContext/use-user.ts +++ b/src/AuthContext/use-user.ts @@ -22,7 +22,6 @@ export default function useUser({ user, updateUser, }: UseAuthData) { - const {mutateAsync: refresh} = useMutation(refreshToken, { onError: () => logout(), }) diff --git a/src/hooks/use-user.ts b/src/hooks/use-user.ts index ef62835..35fb976 100644 --- a/src/hooks/use-user.ts +++ b/src/hooks/use-user.ts @@ -1,5 +1,5 @@ import {useLocation, useNavigate} from "react-router-dom" -import {useContext, useLayoutEffect} from "react" +import {useContext, useEffect, useLayoutEffect} from "react" import {ServerUser, User} from "~/server-types" import AuthContext from "~/AuthContext/AuthContext" @@ -17,7 +17,7 @@ export default function useUser(): ServerUser | User { const navigate = useNavigate() const {user, isAuthenticated} = useContext(AuthContext) - useLayoutEffect(() => { + useEffect(() => { if ( !isAuthenticated && !AUTHENTICATION_PATHS.includes(location.pathname) diff --git a/src/routes/EnterDecryptionPassword.tsx b/src/routes/EnterDecryptionPassword.tsx index 15bebbf..29e63c6 100644 --- a/src/routes/EnterDecryptionPassword.tsx +++ b/src/routes/EnterDecryptionPassword.tsx @@ -36,6 +36,7 @@ export default function EnterDecryptionPassword(): ReactElement { onSubmit: async ({password}, {setErrors}) => { const decryptionPassword = buildEncryptionPassword(password, user.email.address) + console.log("decryptionPassword", decryptionPassword) if (!_setDecryptionPassword(decryptionPassword)) { setErrors({ password: t(