45 lines
1.4 KiB
Zig
45 lines
1.4 KiB
Zig
const std = @import("std");
|
|
const root = @import("root");
|
|
const builtin = @import("builtin");
|
|
const http = @import("http");
|
|
const Uuid = @import("util").Uuid;
|
|
|
|
const RegistrationInfo = @import("../api.zig").RegistrationInfo;
|
|
const utils = @import("../controllers.zig").utils;
|
|
|
|
const RequestServer = root.RequestServer;
|
|
const RouteArgs = http.RouteArgs;
|
|
|
|
pub const login = struct {
|
|
pub const path = "/auth/login";
|
|
pub const method = .POST;
|
|
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
|
|
std.debug.print("{s}", .{ctx.request.body.?});
|
|
const credentials = try utils.parseRequestBody(struct { username: []const u8, password: []const u8 }, ctx);
|
|
defer utils.freeRequestBody(credentials, ctx.alloc);
|
|
|
|
var api = try utils.getApiConn(srv, ctx);
|
|
defer api.close();
|
|
|
|
const token = try api.login(credentials.username, credentials.password);
|
|
|
|
try utils.respondJson(ctx, .ok, token);
|
|
}
|
|
};
|
|
|
|
pub const verify_login = struct {
|
|
pub const path = "/auth/login";
|
|
pub const method = .GET;
|
|
|
|
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
|
|
var api = try utils.getApiConn(srv, ctx);
|
|
defer api.close();
|
|
|
|
// The self-hosted compiler doesn't like inferring this error set.
|
|
// do this for now
|
|
const info = api.getTokenInfo() catch unreachable;
|
|
|
|
try utils.respondJson(ctx, .ok, info);
|
|
}
|
|
};
|