summaryrefslogtreecommitdiff
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
parente02f7ee46876d32ca094c2b5cf3647248baf7645 (diff)
Register error handling
-rw-r--r--backend/src/API/AuthenticationAPI.zig36
-rw-r--r--front/src/Authentication/RegisterPage.tsx20
-rw-r--r--front/src/Models/ErrorResponce.ts3
3 files changed, 26 insertions, 33 deletions
diff --git a/backend/src/API/AuthenticationAPI.zig b/backend/src/API/AuthenticationAPI.zig
index 82cdb1c..ea398ba 100644
--- a/backend/src/API/AuthenticationAPI.zig
+++ b/backend/src/API/AuthenticationAPI.zig
@@ -42,7 +42,9 @@ fn register(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response)
db.Users.Create(&body_model) catch |err| {
if (err == db.ResultErrors.AlreadyExists) {
res.setStatus(.bad_request);
- try res.json(errDesc.AlreadyExistsDescriptor, .{});
+ try res.json(errDesc.ErrorDescriptor {
+ .Message = "The user with that username already exists"
+ }, .{});
return;
}
res.setStatus(.internal_server_error);
@@ -70,10 +72,7 @@ fn login(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response) !vo
return;
};
- const username = body.Username;
- const pwd = body.Password;
-
- const user = db.Users.GetByCredentials(req.arena, username, pwd) catch |err| {
+ const loginResult = loginAction(req.arena, body) catch |err| {
if (err == db.ResultErrors.NotFound) {
res.setStatus(.unauthorized);
try res.json(errDesc.ErrorDescriptor {
@@ -84,30 +83,14 @@ fn login(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response) !vo
return err;
};
- const token = try Tokens.GenerateNewSession(res.arena, user);
- try res.json(.{
- .Token = token,
- .User = .{
- .Username = user.Username,
- .Role = user.Role.ToString(),
- },
- } , .{});
+ try res.json(loginResult, .{});
}
/// Logs in with the given credentials
fn loginAction(allocator: std.mem.Allocator, requetsed: model.RequestBody) !authResult {
const username = requetsed.Username;
const pwd = requetsed.Password;
- const user = try db.Users.GetByCredentials(allocator, username, pwd); // catch |err| {
- // if (err == db.ResultErrors.NotFound) {
- // res.setStatus(.unauthorized);
- // try res.json(errDesc.ErrorDescriptor {
- // .Message = "Login or password is incorrect"
- // }, .{});
- // return;
- // }
- // return err;
- // };
+ const user = try db.Users.GetByCredentials(allocator, username, pwd);
const token = try Tokens.GenerateNewSession(allocator, user);
@@ -118,11 +101,4 @@ fn loginAction(allocator: std.mem.Allocator, requetsed: model.RequestBody) !auth
.Role = user.Role.ToString(),
}
};
- // try res.json(.{
- // .Token = token,
- // .User = .{
- // .Username = user.Username,
- // .Role = user.Role.ToString(),
- // },
- // } , .{});
}
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"
});
}
diff --git a/front/src/Models/ErrorResponce.ts b/front/src/Models/ErrorResponce.ts
new file mode 100644
index 0000000..bc0c552
--- /dev/null
+++ b/front/src/Models/ErrorResponce.ts
@@ -0,0 +1,3 @@
+export type BackendError = {
+ Message: string,
+};