diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-05-03 21:26:49 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-05-03 21:26:49 +0500 |
| commit | e02f7ee46876d32ca094c2b5cf3647248baf7645 (patch) | |
| tree | 7f81450d2ccbcb4fea241a0925b45d9385ae7192 /backend | |
| parent | 533c04d9558bd0a575671c157ca42236f59a2c5a (diff) | |
Register page + logout
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/src/API/AuthenticationAPI.zig | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/backend/src/API/AuthenticationAPI.zig b/backend/src/API/AuthenticationAPI.zig index 0c2a1e9..82cdb1c 100644 --- a/backend/src/API/AuthenticationAPI.zig +++ b/backend/src/API/AuthenticationAPI.zig @@ -23,6 +23,14 @@ fn getUser(data: *Handler.RequestData, _: *httpz.Request, res: *httpz.Response) }, .{}); } +const authResult = struct { + Token: []const u8, + User: struct { + Username: []const u8, + Role: []const u8 + }, +}; + fn register(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response) !void { var body = try req.json(model.RequestBody) orelse { res.setStatus(.bad_request); @@ -40,6 +48,19 @@ fn register(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response) res.setStatus(.internal_server_error); return; }; + + const loginResult = loginAction(req.arena, body) catch |err| { + if (err == db.ResultErrors.NotFound) { + res.setStatus(.unauthorized); + try res.json(errDesc.ErrorDescriptor { + .Message = "Login or password is incorrect" + }, .{}); + return; + } + return err; + }; + + try res.json(loginResult, .{}); res.setStatus(.created); } @@ -72,3 +93,36 @@ fn login(_: *Handler.RequestData, req: *httpz.Request, res: *httpz.Response) !vo }, } , .{}); } + +/// 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 token = try Tokens.GenerateNewSession(allocator, user); + + return authResult { + .Token = token, + .User = .{ + .Username = user.Username, + .Role = user.Role.ToString(), + } + }; + // try res.json(.{ + // .Token = token, + // .User = .{ + // .Username = user.Username, + // .Role = user.Role.ToString(), + // }, + // } , .{}); +} |
