diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 11:54:00 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 11:56:24 +0100 |
| commit | e8ff326f0e5bd61fb0a8c22c48f296fcf0249830 (patch) | |
| tree | 19d8c026f8460ec577f446a7d61e68c38acddde0 /src/routes/api/auth | |
| parent | 007bc3fe0615f38cb31d5116759ed3500f6fd3e6 (diff) | |
backend implement auth-service
Diffstat (limited to 'src/routes/api/auth')
| -rw-r--r-- | src/routes/api/auth/first-login.zig | 5 | ||||
| -rw-r--r-- | src/routes/api/auth/login.zig | 18 | ||||
| -rw-r--r-- | src/routes/api/auth/root.zig | 2 |
3 files changed, 16 insertions, 9 deletions
diff --git a/src/routes/api/auth/first-login.zig b/src/routes/api/auth/first-login.zig index 2fb8c02..34f04d8 100644 --- a/src/routes/api/auth/first-login.zig +++ b/src/routes/api/auth/first-login.zig @@ -1,14 +1,13 @@ const std = @import("std"); const Context = @import("../../context.zig"); -pub const needs_auth = true; -pub const method = .POST; +pub const access = .everyone; const Result = struct { is_first: bool, }; -pub fn handler(ctx: *Context) anyerror!Result { +pub fn post(ctx: *Context) anyerror!Result { _ = ctx; return .{ .is_first = false }; } diff --git a/src/routes/api/auth/login.zig b/src/routes/api/auth/login.zig index c3f2bef..76efcf4 100644 --- a/src/routes/api/auth/login.zig +++ b/src/routes/api/auth/login.zig @@ -3,9 +3,6 @@ const std = @import("std"); const Context = @import("../../context.zig"); const Storage = @import("../../../storage/root.zig"); -pub const needs_auth = false; -pub const method = .POST; - const Body = struct { user: []const u8, password: []const u8, @@ -15,11 +12,22 @@ const Result = struct { success: bool, }; -pub fn handler(ctx: *Context, body: Body) anyerror!Result { +pub const access = .everyone; + +pub fn post(ctx: *Context, body: Body) anyerror!Result { var user = Storage.User.open(ctx.storage, body.user, ctx.allocator) catch return .{ .success = false }; defer user.deinit(); - return .{ .success = user.check_password(body.password) }; + const result = Result { + .success = user.check_password(body.password) + }; + + if (result.success) { + const session = try ctx.storage.sessions.add(ctx.storage, user.info); + ctx.response.headers.fingerprint = session.fingerprint; + } + + return result; } diff --git a/src/routes/api/auth/root.zig b/src/routes/api/auth/root.zig index 785271e..5f45891 100644 --- a/src/routes/api/auth/root.zig +++ b/src/routes/api/auth/root.zig @@ -1,4 +1,4 @@ const HandlerInfo = @import("../../handler-info.zig"); pub const login: HandlerInfo = .from_type(@import("login.zig")); -pub const first_login: HandlerInfo = .from_type(@import("first-login.zig")); +pub const @"first-login": HandlerInfo = .from_type(@import("first-login.zig")); |