From d6cf778053e6a09b49fa64ee643393c615b029e0 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Mon, 3 Oct 2022 22:50:09 -0700 Subject: [PATCH] Move registration to auth --- src/main/api.zig | 20 +++++++++++--------- src/main/main.zig | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/api.zig b/src/main/api.zig index 8f8b991..13c6dd6 100644 --- a/src/main/api.zig +++ b/src/main/api.zig @@ -126,17 +126,15 @@ pub fn setupAdmin(db: sql.Db, origin: []const u8, username: []const u8, password pub const ApiSource = struct { db_conn: *sql.Conn, - internal_alloc: std.mem.Allocator, config: Config, pub const Conn = ApiConn(sql.Db); const root_username = "root"; - pub fn init(alloc: std.mem.Allocator, cfg: Config, db_conn: *sql.Conn) !ApiSource { + pub fn init(cfg: Config, db_conn: *sql.Conn) !ApiSource { return ApiSource{ .db_conn = db_conn, - .internal_alloc = alloc, .config = cfg, }; } @@ -150,7 +148,6 @@ pub const ApiSource = struct { return Conn{ .db = db, - .internal_alloc = self.internal_alloc, .user_id = null, .community = community, .arena = arena, @@ -173,7 +170,6 @@ pub const ApiSource = struct { return Conn{ .db = db, - .internal_alloc = self.internal_alloc, .token_info = token_info, .user_id = token_info.account_id, .community = community, @@ -187,7 +183,6 @@ fn ApiConn(comptime DbConn: type) type { const Self = @This(); db: DbConn, - internal_alloc: std.mem.Allocator, // used *only* for large, internal buffers token_info: ?services.auth.TokenInfo = null, user_id: ?Uuid = null, community: services.communities.Community, @@ -294,7 +289,7 @@ fn ApiConn(comptime DbConn: type) type { pub fn register(self: *Self, request: RegistrationRequest) !UserResponse { std.log.debug("registering user {s} with code {s}", .{ request.username, request.invite_code }); - const invite = try services.invites.getByCode(self.db, request.invite_code, self.arena.allocator()); + const invite = try services.invites.getByCode(self.db, request.invite_code, self.community.id, self.arena.allocator()); if (!Uuid.eql(invite.community_id, self.community.id)) return error.NotFound; if (invite.max_uses != null and invite.times_used >= invite.max_uses.?) return error.InviteExpired; @@ -302,9 +297,16 @@ fn ApiConn(comptime DbConn: type) type { if (self.community.kind == .admin) @panic("Unimplmented"); - const user_id = try services.users.create(self.db, request.username, request.password, self.community.id, .{ .invite_id = invite.id, .email = request.email }, self.internal_alloc); + const user_id = try services.auth.register( + self.db, + request.username, + request.password, + self.community.id, + .{ .invite_id = invite.id, .email = request.email }, + self.arena.allocator(), + ); - switch (invite.invite_type) { + switch (invite.kind) { .user => {}, .system => @panic("System user invites unimplemented"), .community_owner => { diff --git a/src/main/main.zig b/src/main/main.zig index b79394f..527186b 100644 --- a/src/main/main.zig +++ b/src/main/main.zig @@ -24,7 +24,7 @@ const router = Router{ prepare(c.invites.create), - //prepare(c.users.create), + prepare(c.users.create), //prepare(c.notes.create), //prepare(c.notes.get), @@ -141,7 +141,7 @@ pub fn main() !void { //try migrations.up(&db_conn); //try api.setupAdmin(&db_conn, "http://localhost:8080", "root", "password", gpa.allocator()); - var api_src = try api.ApiSource.init(gpa.allocator(), cfg, &db_conn); + var api_src = try api.ApiSource.init(cfg, &db_conn); var srv = try RequestServer.init(gpa.allocator(), &api_src, cfg); return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); }