feat: handle error logs and validate password

This commit is contained in:
lemmyMwaura
2024-08-06 18:36:21 +03:00
parent e420973dd2
commit 3c64359597
4 changed files with 67 additions and 13 deletions
@@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request"; import { apiRequest } from "@app/config/request";
import { TSharedSecret, TViewSharedSecretResponse } from "./types"; import { TSharedSecret, TViewSharedSecretResponse, ValidateSecretPassword } from "./types";
export const secretSharingKeys = { export const secretSharingKeys = {
allSharedSecrets: () => ["sharedSecrets"] as const, allSharedSecrets: () => ["sharedSecrets"] as const,
@@ -67,3 +67,29 @@ export const useGetActiveSharedSecretById = ({
} }
}); });
}; };
export const useValidateSecretPassword = ({
sharedSecretId,
hashedHex,
userPassword,
}: {
sharedSecretId: string;
hashedHex: string;
userPassword: string;
}) => {
return useQuery({
enabled: Boolean(sharedSecretId) && Boolean(userPassword),
queryKey: `validateSecretPass-${sharedSecretId}`,
queryFn: async () => {
const { data, isLoading, refetch } = await apiRequest.post<ValidateSecretPassword>(
`/api/v1/secret-sharing/public/${sharedSecretId}/validate`,
{
hashedHex,
password: userPassword
}
);
return { data, isLoading, refetch }
}
})
}
@@ -42,3 +42,7 @@ export enum SecretSharingAccessType {
Anyone = "anyone", Anyone = "anyone",
Organization = "organization" Organization = "organization"
} }
export type ValidateSecretPassword = {
isValid: boolean
}
@@ -60,7 +60,7 @@ export const ViewSecretPublicPage = () => {
</div> </div>
{secret && ( {secret && (
!passMatches && secret.password ? ( !passMatches && secret.password ? (
<PasswordContainer secret={secret} handlePassMatch={handlePassMatch} /> <PasswordContainer secretId={id} hashedHex={hashedHex} handlePassMatch={handlePassMatch} />
) : ( ) : (
key && <SecretContainer secret={secret} secretKey={key} /> key && <SecretContainer secret={secret} secretKey={key} />
) )
@@ -1,27 +1,51 @@
import { useState, ChangeEvent } from "react"; import { useState, ChangeEvent } from "react";
import {
faArrowRight, import { faArrowRight } from "@fortawesome/free-solid-svg-icons";
} from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon, faSpinner } from "@fortawesome/react-fontawesome";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Button, IconButton, Input } from "@app/components/v2"; import { Button, IconButton, Input } from "@app/components/v2";
import { TViewSharedSecretResponse } from "@app/hooks/api/secretSharing"; import { useValidateSecretPassword } from "@app/hooks/api/secretSharing";
import { createNotification } from "@app/components/notifications";
type Props = { type Props = {
secret: TViewSharedSecretResponse; secretId: string;
hashedHex: string;
handlePassMatch: (val: boolean) => void; handlePassMatch: (val: boolean) => void;
}; };
export const PasswordContainer = ({ secret, handlePassMatch }: Props) => { export const PasswordContainer = ({ secretId, hashedHex, handlePassMatch }: Props) => {
const [password, setPassword] = useState<string>('') const [password, setPassword] = useState<string>('')
const [isLoading, setLoading] = useState(false)
const { refetch } = useValidateSecretPassword({
sharedSecretId: secretId,
hashedHex,
userPassword: password,
})
const handleChange = (e: ChangeEvent<HTMLInputElement>) => { const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setPassword(e.target.value) setPassword(e.target.value)
} }
const validatePassword = () => { const validatePassword = async () => {
if (secret.password === password) { try {
handlePassMatch(true) const { data: freshData } = await refetch();
if (freshData.data.isValid === true) {
handlePassMatch(true);
} else {
createNotification({
text: "Password is Invalid. Try again",
type: "error"
})
}
} catch (error) {
console.error("Failed to validate password:", error);
createNotification({
text: "Failed to validate password",
type: "error"
})
} finally {
setLoading(false);
} }
} }
@@ -38,7 +62,7 @@ export const PasswordContainer = ({ secret, handlePassMatch }: Props) => {
validatePassword() validatePassword()
}} }}
> >
<FontAwesomeIcon icon={faArrowRight} /> <FontAwesomeIcon icon={isLoading ? faSpinner : faArrowRight} />
</IconButton> </IconButton>
</div> </div>
</div> </div>