diff --git a/demo/src/app/views/login.zig b/demo/src/app/views/login.zig new file mode 100644 index 0000000..f975458 --- /dev/null +++ b/demo/src/app/views/login.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const jetzig = @import("jetzig"); +const auth = @import("jetzig").auth; + +pub fn index(request: *jetzig.Request) !jetzig.View { + return request.render(.ok); +} + +pub fn post(request: *jetzig.Request) !jetzig.View { + const Login = struct { + email: []const u8, + password: []const u8, + }; + + const params = try request.expectParams(Login) orelse { + return request.fail(.forbidden); + }; + + // Lookup the user by email + const query = jetzig.database.Query(.User).findBy( + .{ .email = params.email }, + ); + + const user = try request.repo.execute(query) orelse { + return request.fail(.forbidden); + }; + + // Check that the password matches + if (try auth.verifyPassword( + request.allocator, + user.password_hash, + params.password, + )) { + try auth.signIn(request, user.id); + return request.redirect("/", .found); + } + return request.fail(.forbidden); +} diff --git a/demo/src/app/views/login/index.zmpl b/demo/src/app/views/login/index.zmpl new file mode 100644 index 0000000..fc3fc9d --- /dev/null +++ b/demo/src/app/views/login/index.zmpl @@ -0,0 +1,7 @@ +
+ + + + + +