From cc4badae218b526323af6229d897cc7f947fdccb Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sun, 1 Jan 2023 23:34:30 -0800 Subject: [PATCH] refactor --- src/api/methods/auth.zig | 23 +++++------------------ src/api/services/tokens.zig | 9 ++++++--- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/api/methods/auth.zig b/src/api/methods/auth.zig index 3ec9e11..2847953 100644 --- a/src/api/methods/auth.zig +++ b/src/api/methods/auth.zig @@ -4,7 +4,6 @@ const types = @import("../types.zig"); const Uuid = util.Uuid; const DateTime = util.DateTime; -const UserResponse = @import("../lib.zig").UserResponse; const Invite = @import("../lib.zig").Invite; pub const Token = types.Token; @@ -131,7 +130,7 @@ pub fn methods(comptime models: type) type { try models.tokens.create(tx, credentials.account_id, token_hash, self.allocator); try tx.commit(); - const info = try models.tokens.getByHash(self.db, token_hash, self.allocator); + const info = try models.tokens.getByHash(self.db, token_hash, community_id, self.allocator); defer util.deepFree(self.allocator, info); return .{ @@ -147,22 +146,10 @@ pub fn methods(comptime models: type) type { const hash = try hashToken(token, self.allocator); defer self.allocator.free(hash); - return self.db.queryRow( - Token.Info, - \\SELECT token.account_id as user_id, token.issued_at - \\FROM token - \\ JOIN account - \\ JOIN actor - \\ ON token.account_id = account.id AND account.id = actor.id - \\WHERE token.hash = $1 AND actor.community_id = $2 - \\LIMIT 1 - , - .{ hash, self.context.community.id }, - self.allocator, - ) catch |err| switch (err) { - error.NoRows => error.InvalidToken, - else => error.DatabaseFailure, - }; + const info = try models.tokens.getByHash(self.db, hash, self.context.community.id, self.allocator); + defer util.deepFree(self.allocator, info); + + return .{ .user_id = info.account_id, .issued_at = info.issued_at }; } }; } diff --git a/src/api/services/tokens.zig b/src/api/services/tokens.zig index 7290432..1ed0a5b 100644 --- a/src/api/services/tokens.zig +++ b/src/api/services/tokens.zig @@ -20,15 +20,18 @@ pub fn create(db: anytype, account_id: Uuid, hash: []const u8, alloc: std.mem.Al }, alloc); } -pub fn getByHash(db: anytype, hash: []const u8, alloc: std.mem.Allocator) !Token { +pub fn getByHash(db: anytype, hash: []const u8, community_id: Uuid, alloc: std.mem.Allocator) !Token { return db.queryRow( Token, \\SELECT account_id, issued_at, hash \\FROM token - \\WHERE hash = $1 + \\ JOIN account + \\ JOIN actor + \\ ON token.account_id = account.id AND account.id = actor.id + \\WHERE token.hash = $1 AND actor.community_id = $2 \\LIMIT 1 , - .{hash}, + .{ hash, community_id }, alloc, ) catch |err| switch (err) { error.NoRows => error.InvalidToken,