Viewing:
const httpz = @import("httpz");
const App = @import("App.zig");
const form = @import("form.zig");
pub fn doSignup(app: *App, req: *httpz.Request, res: *httpz.Response) ![]const u8 {
std.log.debug("do signup", .{});
const username = try form.Req(req, res, "username");
const password = try form.Req(req, res, "password");
if (!std.mem.eql(u8, app.cfg.signup_code, "")) {
if (!std.mem.eql(u8, app.cfg.signup_code, try form.Req(req, res, "code"))) {
return form.Err.MismatchedValue;
}
}
const hashed = try res.arena.alloc(u8, 32);
const salt = pad.randomString(app.rng, res.arena, app.cfg.salt_length);
try toPw(hashed, password, salt, app.cfg.pbkdf2Rounds);
const stored_password: []const u8 = try std.fmt.allocPrint(res.arena, "{d}:{s}:{s}", .{ app.cfg.pbkdf2Rounds, salt, hashed });
try app.db.addUser(username, stored_password);
const random = getRandom(app.io);
const token: []u8 = pad.randomString(random, res.arena, 32);
const token_const: []const u8 = token[0..];
const expires: usize = @as(usize, @intCast(std.Io.Timestamp.now(app.io, .real).toSeconds())) + app.cfg.session_length;
std.log.debug("expires: {d}", .{expires});
try app.db.addSession(username, token_const, expires);
std.log.debug("try set cookie", .{});
return token_const;
try res.setCookie("Session", token_const, .{
.path = "/",
.max_age = @as(i32, @intCast(app.cfg.session_length)),
.secure = true,
.http_only = true,
.partitioned = true,
.same_site = .lax, // or .none, or .strict (or null to leave out)
});
goHome(app, res);
}