Skip to content

Commit

Permalink
example of how to use validation.now
Browse files Browse the repository at this point in the history
  • Loading branch information
softprops committed Jul 31, 2024
1 parent f036b2c commit 4a259b8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,21 @@ pub fn decode(

test "roundtrip" {
const allocator = std.testing.allocator;
const token = try encode(allocator, .{ .alg = .HS256 }, .{ .sub = "test", .exp = @divTrunc(std.time.milliTimestamp(), 1000) * 10 }, .{ .secret = "secret" });
const validation: Validation = .{
.now = struct {
fn func() u64 {
return 1722441274; // Wednesday, July 31, 2024 3:54:34 PM - in seconds
}
}.func,
};
const token = try encode(allocator, .{ .alg = .HS256 }, .{ .sub = "test", .exp = validation.now() + 60 }, .{ .secret = "secret" });
defer allocator.free(token);
var jwt = try decode(
std.testing.allocator,
struct { sub: []const u8 },
token,
.{ .secret = "secret" },
.{},
validation,
);
defer jwt.deinit();
try std.testing.expectEqualStrings("test", jwt.claims.sub);
Expand Down

0 comments on commit 4a259b8

Please sign in to comment.