-
Notifications
You must be signed in to change notification settings - Fork 47
Add an example for using auth for logging in #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9012ec3
Add an example for using auth for logging in
sc68cal b070d28
Add user table
sc68cal 58f7e0e
Add User to schema
sc68cal f378856
Fix argument to first migration
sc68cal 22688cc
Set adapter to postgresql
sc68cal c6ce935
Run postgres inside the test runner
sc68cal 48e7139
Set JETQUERY_POOL_SIZE in CI runs
sc68cal 46a3111
Use actual YAML syntax for env vars
sc68cal f94db09
Do not pre-create database
sc68cal 70a8b0f
Add tests for login view
sc68cal 359b4fc
Add migration step
sc68cal 9e4e852
Create a template file for init
sc68cal b81c6df
update paths to database template
sc68cal 21bf237
Shrink database.zig and comment
sc68cal aae8909
fix spelling mistake
sc68cal 79574ed
Use defer to free hashed_pass
sc68cal 2cb1b4e
Adjust test, fix memory leak in hashPassword, deinit repo on test app…
bobf 327b039
Merge pull request #1 from bobf/login_example
sc68cal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,7 @@ | ||
pub const database = .{ | ||
// Null adapter fails when a database call is invoked. | ||
.development = .{ | ||
.adapter = .null, | ||
}, | ||
// This configuration is used for CI | ||
// in GitHub | ||
.testing = .{ | ||
.adapter = .null, | ||
}, | ||
.production = .{ | ||
.adapter = .null, | ||
.adapter = .postgresql, | ||
}, | ||
// PostgreSQL adapter configuration. | ||
// | ||
// All options except `adapter` can be configured using environment variables: | ||
// | ||
// * JETQUERY_HOSTNAME | ||
// * JETQUERY_PORT | ||
// * JETQUERY_USERNAME | ||
// * JETQUERY_PASSWORD | ||
// * JETQUERY_DATABASE | ||
// | ||
// .testing = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_testing", | ||
// }, | ||
// | ||
// .development = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_development", | ||
// }, | ||
// | ||
// .production = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_production", | ||
// }, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
pub const database = .{ | ||
// Null adapter fails when a database call is invoked. | ||
.development = .{ | ||
.adapter = .null, | ||
}, | ||
.testing = .{ | ||
.adapter = .null, | ||
}, | ||
.production = .{ | ||
.adapter = .null, | ||
}, | ||
// PostgreSQL adapter configuration. | ||
// | ||
// All options except `adapter` can be configured using environment variables: | ||
// | ||
// * JETQUERY_HOSTNAME | ||
// * JETQUERY_PORT | ||
// * JETQUERY_USERNAME | ||
// * JETQUERY_PASSWORD | ||
// * JETQUERY_DATABASE | ||
// | ||
// .testing = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_testing", | ||
// }, | ||
// | ||
// .development = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_development", | ||
// }, | ||
// | ||
// .production = .{ | ||
// .adapter = .postgresql, | ||
// .hostname = "localhost", | ||
// .port = 5432, | ||
// .username = "postgres", | ||
// .password = "password", | ||
// .database = "myapp_production", | ||
// }, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const jetquery = @import("jetzig").jetquery; | ||
|
||
pub const User = jetquery.Model(@This(), "users", struct { | ||
id: i32, | ||
email: []const u8, | ||
password_hash: []const u8, | ||
created_at: jetquery.DateTime, | ||
updated_at: jetquery.DateTime, | ||
}, .{}); |
4 changes: 2 additions & 2 deletions
4
demo/src/app/database/migrations/2024-08-25_13-18-52_hello.zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
const jetquery = @import("jetquery"); | ||
|
||
pub fn up(repo: *jetquery.Repo) !void { | ||
pub fn up(repo: anytype) !void { | ||
_ = repo; | ||
} | ||
|
||
pub fn down(repo: *jetquery.Repo) !void { | ||
pub fn down(repo: anytype) !void { | ||
_ = repo; | ||
} |
20 changes: 20 additions & 0 deletions
20
demo/src/app/database/migrations/2025-03-10_01-36-58_create_users.zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const std = @import("std"); | ||
const jetquery = @import("jetquery"); | ||
const t = jetquery.schema.table; | ||
|
||
pub fn up(repo: anytype) !void { | ||
try repo.createTable( | ||
"users", | ||
&.{ | ||
t.primaryKey("id", .{}), | ||
t.column("email", .string, .{ .unique = true, .index = true }), | ||
t.column("password_hash", .string, .{}), | ||
t.timestamps(.{}), | ||
}, | ||
.{}, | ||
); | ||
} | ||
|
||
pub fn down(repo: anytype) !void { | ||
try repo.dropTable("users", .{}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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); | ||
} | ||
|
||
test "post" { | ||
var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); | ||
defer app.deinit(); | ||
|
||
const hashed_pass = try auth.hashPassword(std.testing.allocator, "test"); | ||
defer std.testing.allocator.free(hashed_pass); | ||
|
||
try jetzig.database.Query(.User).deleteAll().execute(app.repo); | ||
try app.repo.insert(.User, .{ | ||
.id = 1, | ||
.email = "[email protected]", | ||
.password_hash = hashed_pass, | ||
}); | ||
|
||
const response = try app.request(.POST, "/login", .{ | ||
.json = .{ | ||
.email = "[email protected]", | ||
.password = "test", | ||
}, | ||
}); | ||
try response.expectStatus(.found); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<form method="post" id="login"> | ||
<input type="email" name="email" placeholder="[email protected]"> | ||
<label for="email">Email address</label> | ||
<input type="password" name="password" placeholder="Password"> | ||
<label for="password">Password</label> | ||
<button type="submit" form="login">Sign in</button> | ||
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.