Pinned 2026 toolchains (Go 1.26, Rust 1.98/edition 2024, Python 3.14 + uv, Node 24, Zig 0.16, NixOS 26.05), postgres 18 / mongo 8, lockfiles built from, non-root runtimes, .dockerignore, per-project LICENSE, READMEs with the git.devai.io clone line, checkout@v7 CI. Security fixes in the legacy Rust APIs (any-password login, self-assigned admin, hard-coded JWT secret), JWT alg/exp/sub enforcement across the blog series, safe markdown links in the frontends, and many smaller bugs — every project was built, run and exercised end to end. Adds scripts/publish.sh + a CI publish job that splits every folder into its own repo at git.devai.io/templates/<folder>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01128fhuZbgivaSJvtMf4s1G
2.6 KiB
Auth via Clerk
clerk.zig replaces the local email+password auth with
Clerk: the app stops issuing tokens and instead verifies
Clerk session JWTs (RS256) against your instance's JWKS — signature, exp and
nbf — using only the standard library.
Setup
-
Copy
clerk.zigintosrc/and deletesrc/jwt.zig. -
src/auth.zigshrinks torequireAuth(dropregister,login,normalizeEmailand the argon2/jwt imports):pub fn requireAuth(app: *App, arena: Allocator, req: *http.Server.Request) ![]const u8 { const token = web.bearerToken(req) orelse return error.Unauthorized; return app.verifier.verify(arena, token) catch error.Unauthorized; } -
In
src/main.zig, remove the/auth/registerand/auth/loginroutes, addconst clerk = @import("clerk.zig");, replace theauth_secretfield ofAppwithverifier: *clerk.Verifier, and replace theAUTH_SECRETlookup inmainwith:const jwks_url = env.get("CLERK_JWKS_URL") orelse std.process.fatal("CLERK_JWKS_URL is required", .{}); var verifier = clerk.Verifier.init(gpa, io, jwks_url); defer verifier.deinit();and set
.verifier = &verifierwhereappis built. -
Clerk user ids are strings (
user_...), not bigints. Insrc/db.zig, makeauthor_ida[]const u8inPostand increatePost, read it withtry arena.dupe(u8, try row.get([]const u8, 5))inpostRow, and deleteUser,createUserandgetUserByEmail. Insrc/posts.zig,findOwnPosttakesuser_id: []const u8and compares withstd.mem.eql(u8, post.author_id, user_id). -
In
schema.sql, delete theuserstable and change the column toauthor_id text not null(create table if not existswon't alter an existing table, so start from an empty database). -
Set
CLERK_JWKS_URL=https://<your-instance>.clerk.accounts.dev/.well-known/jwks.json(Clerk dashboard → API keys) instead ofAUTH_SECRET.
Clients send the Clerk session token as Authorization: Bearer <token>
(await session.getToken() in Clerk's frontend SDKs).
Notes
- The JWKS is fetched over HTTPS with
std.http.Client, which needs the system CA bundle — the distroless runtime image ships one. - Keys are cached in memory. A token with an unknown
kidtriggers a refetch (at most once a minute), so key rotations need no restart. - Clerk also puts the requesting origin in the
azpclaim; if browsers from other sites could hold your users' tokens, parseazpinverifyand compare it with your frontend's origin.