mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-20 00:05:26 +02:00
improving AuthContextProvider
This commit is contained in:
parent
caa1a108d8
commit
c89ad41602
@ -8,7 +8,7 @@ export interface UseContextValueData {
|
|||||||
encryptUsingMasterPassword: (content: string) => string
|
encryptUsingMasterPassword: (content: string) => string
|
||||||
decryptUsingMasterPassword: (content: string) => string
|
decryptUsingMasterPassword: (content: string) => string
|
||||||
decryptUsingPrivateKey: (message: string) => Promise<string>
|
decryptUsingPrivateKey: (message: string) => Promise<string>
|
||||||
setDecryptionPassword: (password: string) => void
|
setDecryptionPassword: (password: string) => boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function useContextValue({
|
export default function useContextValue({
|
||||||
|
@ -10,7 +10,7 @@ export interface UseMasterPasswordResult {
|
|||||||
decryptUsingMasterPassword: (content: string) => string
|
decryptUsingMasterPassword: (content: string) => string
|
||||||
decryptUsingPrivateKey: (message: string) => Promise<string>
|
decryptUsingPrivateKey: (message: string) => Promise<string>
|
||||||
|
|
||||||
setDecryptionPassword: (password: string) => void
|
setDecryptionPassword: (password: string) => boolean
|
||||||
logout: () => void
|
logout: () => void
|
||||||
// Use this cautiously
|
// Use this cautiously
|
||||||
_masterPassword: string
|
_masterPassword: string
|
||||||
@ -78,6 +78,22 @@ export default function useMasterPassword(
|
|||||||
[user],
|
[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(() => {
|
const logout = useCallback(() => {
|
||||||
setDecryptionPassword(null)
|
setDecryptionPassword(null)
|
||||||
}, [])
|
}, [])
|
||||||
@ -86,8 +102,8 @@ export default function useMasterPassword(
|
|||||||
encryptUsingMasterPassword,
|
encryptUsingMasterPassword,
|
||||||
decryptUsingMasterPassword,
|
decryptUsingMasterPassword,
|
||||||
decryptUsingPrivateKey,
|
decryptUsingPrivateKey,
|
||||||
setDecryptionPassword,
|
|
||||||
logout,
|
logout,
|
||||||
|
setDecryptionPassword: updateDecryptionPassword,
|
||||||
_masterPassword: masterPassword!,
|
_masterPassword: masterPassword!,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,6 @@ export default function useUser({
|
|||||||
user,
|
user,
|
||||||
updateUser,
|
updateUser,
|
||||||
}: UseAuthData) {
|
}: UseAuthData) {
|
||||||
|
|
||||||
const {mutateAsync: refresh} = useMutation<RefreshTokenResult, AxiosError, void>(refreshToken, {
|
const {mutateAsync: refresh} = useMutation<RefreshTokenResult, AxiosError, void>(refreshToken, {
|
||||||
onError: () => logout(),
|
onError: () => logout(),
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {useLocation, useNavigate} from "react-router-dom"
|
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 {ServerUser, User} from "~/server-types"
|
||||||
import AuthContext from "~/AuthContext/AuthContext"
|
import AuthContext from "~/AuthContext/AuthContext"
|
||||||
@ -17,7 +17,7 @@ export default function useUser(): ServerUser | User {
|
|||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const {user, isAuthenticated} = useContext(AuthContext)
|
const {user, isAuthenticated} = useContext(AuthContext)
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
!isAuthenticated &&
|
!isAuthenticated &&
|
||||||
!AUTHENTICATION_PATHS.includes(location.pathname)
|
!AUTHENTICATION_PATHS.includes(location.pathname)
|
||||||
|
@ -36,6 +36,7 @@ export default function EnterDecryptionPassword(): ReactElement {
|
|||||||
onSubmit: async ({password}, {setErrors}) => {
|
onSubmit: async ({password}, {setErrors}) => {
|
||||||
const decryptionPassword = buildEncryptionPassword(password, user.email.address)
|
const decryptionPassword = buildEncryptionPassword(password, user.email.address)
|
||||||
|
|
||||||
|
console.log("decryptionPassword", decryptionPassword)
|
||||||
if (!_setDecryptionPassword(decryptionPassword)) {
|
if (!_setDecryptionPassword(decryptionPassword)) {
|
||||||
setErrors({
|
setErrors({
|
||||||
password: t(
|
password: t(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user