mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Add an example for using auth for logging in
This commit is contained in:
parent
182ceee17f
commit
9012ec3aaf
38
demo/src/app/views/login.zig
Normal file
38
demo/src/app/views/login.zig
Normal file
@ -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);
|
||||
}
|
7
demo/src/app/views/login/index.zmpl
Normal file
7
demo/src/app/views/login/index.zmpl
Normal file
@ -0,0 +1,7 @@
|
||||
<form method="post" id="login">
|
||||
<input type="email" name="email" placeholder="name@example.com">
|
||||
<label for="email">Email address</label>
|
||||
<input type="password" name="password" placeholder="Password">
|
||||
<label for="paddword">Password</label>
|
||||
<button type="submit" form="login">Sign in</button>
|
||||
</form>
|
Loading…
x
Reference in New Issue
Block a user