summaryrefslogtreecommitdiff
path: root/front/src/Authentication/RegisterPage.tsx
diff options
context:
space:
mode:
authorphyscik <mynameisgennadiy@vk.com>2026-05-04 12:22:25 +0500
committerphyscik <mynameisgennadiy@vk.com>2026-05-04 12:22:25 +0500
commitb82265994c5f72c40f68c6b3a4960869b6a67951 (patch)
treef2e7524ffded1de4bc29d3b4c5c4a7236111129e /front/src/Authentication/RegisterPage.tsx
parente02f7ee46876d32ca094c2b5cf3647248baf7645 (diff)
Register error handling
Diffstat (limited to 'front/src/Authentication/RegisterPage.tsx')
-rw-r--r--front/src/Authentication/RegisterPage.tsx20
1 files changed, 17 insertions, 3 deletions
diff --git a/front/src/Authentication/RegisterPage.tsx b/front/src/Authentication/RegisterPage.tsx
index b30b182..5c818f6 100644
--- a/front/src/Authentication/RegisterPage.tsx
+++ b/front/src/Authentication/RegisterPage.tsx
@@ -1,10 +1,11 @@
-import axios from "axios";
+import axios, { AxiosError } from "axios";
import { useContext, useState } from "react";
import { useNavigate } from "react-router";
import { BackendURL } from "../Config";
import { Authentication, SaveState } from "./ContextProvider";
import { GetLocalizedString } from "../Locales/Locales";
import { LanguageContext } from "../Locales/Context";
+import { BackendError } from "../Models/ErrorResponce";
const RegisterURL = `${BackendURL}/auth/register`;
@@ -13,6 +14,7 @@ function RegisterPage() {
const [username, setUsername] = useState<string>("");
const [passw, setPassw] = useState<string>("");
+ const [errMessage, setErrorMessage] = useState<string>("");
const navigate = useNavigate();
function SetAuthState(newAuthState: Authentication | null) {
@@ -37,13 +39,17 @@ function RegisterPage() {
SetAuthState(data);
navigate("/");
window.location.reload();
+ }, (err) => {
+ setErrorMessage(err);
+ console.log(err);
});
}}> { GetLocalizedString("Register", lang) } </button>
+ <p> { errMessage } </p>
</div>
);
}
-async function Register(username: string, passw: string, onSuccess: (data: Authentication) => void) {
+async function Register(username: string, passw: string, onSuccess: (data: Authentication) => void, onError: (message: string) => void) {
await axios.post<Authentication>(
RegisterURL, {
Username: username,
@@ -52,7 +58,15 @@ async function Register(username: string, passw: string, onSuccess: (data: Authe
).then(resp => {
onSuccess(resp.data);
}).catch(err => {
- console.log(`Failed to send a login responce: ${err}`);
+ if (axios.isAxiosError(err)) {
+ const parsedErr = err as AxiosError<BackendError>;
+ if (parsedErr.response) {
+ console.log(parsedErr.response);
+ onError(parsedErr.response.data.Message);
+ return
+ }
+ }
+ return "An unexpected error occured"
});
}