Move registration to auth

This commit is contained in:
jaina heartles 2022-10-03 22:50:09 -07:00
parent 753ae2729e
commit d6cf778053
2 changed files with 13 additions and 11 deletions

View file

@ -126,17 +126,15 @@ pub fn setupAdmin(db: sql.Db, origin: []const u8, username: []const u8, password
pub const ApiSource = struct { pub const ApiSource = struct {
db_conn: *sql.Conn, db_conn: *sql.Conn,
internal_alloc: std.mem.Allocator,
config: Config, config: Config,
pub const Conn = ApiConn(sql.Db); pub const Conn = ApiConn(sql.Db);
const root_username = "root"; 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{ return ApiSource{
.db_conn = db_conn, .db_conn = db_conn,
.internal_alloc = alloc,
.config = cfg, .config = cfg,
}; };
} }
@ -150,7 +148,6 @@ pub const ApiSource = struct {
return Conn{ return Conn{
.db = db, .db = db,
.internal_alloc = self.internal_alloc,
.user_id = null, .user_id = null,
.community = community, .community = community,
.arena = arena, .arena = arena,
@ -173,7 +170,6 @@ pub const ApiSource = struct {
return Conn{ return Conn{
.db = db, .db = db,
.internal_alloc = self.internal_alloc,
.token_info = token_info, .token_info = token_info,
.user_id = token_info.account_id, .user_id = token_info.account_id,
.community = community, .community = community,
@ -187,7 +183,6 @@ fn ApiConn(comptime DbConn: type) type {
const Self = @This(); const Self = @This();
db: DbConn, db: DbConn,
internal_alloc: std.mem.Allocator, // used *only* for large, internal buffers
token_info: ?services.auth.TokenInfo = null, token_info: ?services.auth.TokenInfo = null,
user_id: ?Uuid = null, user_id: ?Uuid = null,
community: services.communities.Community, community: services.communities.Community,
@ -294,7 +289,7 @@ fn ApiConn(comptime DbConn: type) type {
pub fn register(self: *Self, request: RegistrationRequest) !UserResponse { pub fn register(self: *Self, request: RegistrationRequest) !UserResponse {
std.log.debug("registering user {s} with code {s}", .{ request.username, request.invite_code }); 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 (!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; 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"); 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 => {}, .user => {},
.system => @panic("System user invites unimplemented"), .system => @panic("System user invites unimplemented"),
.community_owner => { .community_owner => {

View file

@ -24,7 +24,7 @@ const router = Router{
prepare(c.invites.create), prepare(c.invites.create),
//prepare(c.users.create), prepare(c.users.create),
//prepare(c.notes.create), //prepare(c.notes.create),
//prepare(c.notes.get), //prepare(c.notes.get),
@ -141,7 +141,7 @@ pub fn main() !void {
//try migrations.up(&db_conn); //try migrations.up(&db_conn);
//try api.setupAdmin(&db_conn, "http://localhost:8080", "root", "password", gpa.allocator()); //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); var srv = try RequestServer.init(gpa.allocator(), &api_src, cfg);
return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
} }