Accept password at command line for auth user create

Intended for local development only
This commit is contained in:
Bob Farrell 2024-11-13 20:35:50 +00:00
parent f9c6f9f38f
commit c8ae44b508
3 changed files with 35 additions and 21 deletions

View File

@ -349,6 +349,9 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
if (b.option([]const u8, "auth_username", "Auth username")) |username| {
run_auth_user_create_cmd.addArg(username);
}
if (b.option([]const u8, "auth_password", "Auth password")) |password| {
run_auth_user_create_cmd.addArg(password);
}
const exe_database = b.addExecutable(.{
.name = "database",

View File

@ -60,13 +60,16 @@ pub fn run(
std.debug.print("Missing argument. Expected an email/username parameter.\n", .{});
break :blk error.JetzigCommandError;
} else {
try util.execCommand(allocator, &.{
"zig",
"build",
util.environmentBuildOption(main_options.options.environment),
try std.mem.concat(allocator, u8, &.{ "-Dauth_username=", sub_args[0] }),
"jetzig:auth:user:create",
});
var argv = std.ArrayList([]const u8).init(allocator);
try argv.append("zig");
try argv.append("build");
try argv.append(util.environmentBuildOption(main_options.options.environment));
try argv.append(try std.mem.concat(allocator, u8, &.{ "-Dauth_username=", sub_args[0] }));
if (sub_args.len > 1) {
try argv.append(try std.mem.concat(allocator, u8, &.{ "-Dauth_password=", sub_args[1] }));
}
try argv.append("jetzig:auth:user:create");
try util.execCommand(allocator, argv.items);
}
},
};

View File

@ -42,21 +42,29 @@ pub fn main() !void {
const stdin = std.io.getStdIn();
const reader = stdin.reader();
if (stdin.isTty()) std.debug.print("Enter password: ", .{});
var buf: [1024]u8 = undefined;
if (try reader.readUntilDelimiterOrEof(&buf, '\n')) |input| {
const password = std.mem.trim(u8, input, &std.ascii.whitespace);
const email = args[2];
try repo.insert(std.enums.nameCast(std.meta.DeclEnum(Schema), model), .{
.email = email,
.password_hash = try hashPassword(allocator, password),
});
std.debug.print("Created user: `{s}`.\n", .{email});
} else {
const password = if (stdin.isTty() and args.len < 4) blk: {
std.debug.print("Enter password: ", .{});
var buf: [1024]u8 = undefined;
if (try reader.readUntilDelimiterOrEof(&buf, '\n')) |input| {
break :blk std.mem.trim(u8, input, &std.ascii.whitespace);
} else {
std.debug.print("Blank password. Exiting.\n", .{});
return;
}
} else if (args.len >= 4)
args[3]
else {
std.debug.print("Blank password. Exiting.\n", .{});
}
return;
};
const email = args[2];
try repo.insert(std.enums.nameCast(std.meta.DeclEnum(Schema), model), .{
.email = email,
.password_hash = try hashPassword(allocator, password),
});
std.debug.print("Created user: `{s}`.\n", .{email});
},
}
}