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 {
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 => {

View File

@ -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);
}