From b82265994c5f72c40f68c6b3a4960869b6a67951 Mon Sep 17 00:00:00 2001 From: physcik Date: Mon, 4 May 2026 12:22:25 +0500 Subject: Register error handling --- backend/src/API/AuthenticationAPI.zig | 36 ++++++------------------------- front/src/Authentication/RegisterPage.tsx | 20 ++++++++++++++--- front/src/Models/ErrorResponce.ts | 3 +++ 3 files changed, 26 insertions(+), 33 deletions(-) create mode 100644 front/src/Models/ErrorResponce.ts 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(""); const [passw, setPassw] = useState(""); + const [errMessage, setErrorMessage] = useState(""); 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) } +

{ errMessage }

); } -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( 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; + 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, +}; -- cgit v1.3