mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-19 07:55:25 +02:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import {AuthContextType, EncryptionStatus} from "~/AuthContext/AuthContext"
|
|
import {ServerUser, User} from "~/server-types"
|
|
|
|
export interface UseContextValueData {
|
|
user: User | ServerUser | null
|
|
login: (user: User | ServerUser) => void
|
|
logout: () => void
|
|
encryptUsingMasterPassword: (content: string) => string
|
|
decryptUsingMasterPassword: (content: string) => string
|
|
decryptUsingPrivateKey: (message: string) => Promise<string>
|
|
setDecryptionPassword: (password: string) => boolean
|
|
}
|
|
|
|
export default function useContextValue({
|
|
user,
|
|
login,
|
|
logout,
|
|
encryptUsingMasterPassword,
|
|
decryptUsingMasterPassword,
|
|
setDecryptionPassword,
|
|
decryptUsingPrivateKey,
|
|
}: UseContextValueData): AuthContextType {
|
|
return {
|
|
user,
|
|
login,
|
|
logout,
|
|
isAuthenticated: Boolean(user),
|
|
encryptionStatus: (() => {
|
|
if (!user) {
|
|
return EncryptionStatus.Unavailable
|
|
}
|
|
|
|
if (!user.encryptedPassword) {
|
|
return EncryptionStatus.Unavailable
|
|
}
|
|
|
|
if (user.isDecrypted) {
|
|
return EncryptionStatus.Available
|
|
}
|
|
|
|
return EncryptionStatus.PasswordRequired
|
|
})(),
|
|
_updateUser: login,
|
|
_setDecryptionPassword: setDecryptionPassword,
|
|
_encryptUsingMasterPassword: encryptUsingMasterPassword,
|
|
_decryptUsingMasterPassword: decryptUsingMasterPassword,
|
|
_decryptUsingPrivateKey: decryptUsingPrivateKey,
|
|
}
|
|
}
|