We are launching all new Klivolks 3.0See what is new

Authentication

Authentication (-auth)

none jwt session both

middleware.RequireAuth — what every -protected route wraps its handlers in — is generated differently per -auth choice. jwt generates auth/jwt.go (stdlib-only HS256, IssueJWT/VerifyJWT). session generates auth/session.go — an in-memory map behind a mutex plus an HttpOnly/SameSite=Lax cookie (StartSession/SessionFromRequest/EndSession); lost on restart, not shared across instances by design. both generates both, and RequireAuth tries the bearer token first, falling back to the session cookie — one protected route serves API and browser clients at once.

Whichever mechanism verifies the caller, the resulting subject (userID) is attached to the request's context via auth.ContextWithSubject; any protected handler reads it back with auth.Subject(r) (subject string, ok bool). No /login//logout route is ever generated — call auth.IssueJWT/auth.StartSession from your own login handler once credentials are verified. For -auth jwt|both apps with -db mongo, a real JWT revocation primitive is generated (core/tokens.go's RevokeAllTokens/IsTokenRevoked, wired into RequireAuth automatically) — so a hand-written logout handler has something real to call instead of building its own blocklist; see the Changelog for details.

Usage — a login handler you write yourself (scaffolded separately, e.g. nexler create /login -module auth), calling the generated primitives once credentials check out:

// -auth jwt or both
token, err := auth.IssueJWT(userID, name, org, userType, userRole, 24*time.Hour)
response.JSON(w, http.StatusOK, LoginResponse{Token: token})

// -auth session or both
auth.StartSession(w, userID, false)  // rememberMe if -remember-me was set
response.JSON(w, http.StatusOK, LoginResponse{})

// any protected handler downstream
userID, ok := auth.Subject(r)

Multi-tenant Org propagation (-multitenant)

Only with -auth jwt|session|both — errors if combined with -auth none, since there's no authenticated subject to attach an Org to. Threads a tenant Org end to end: auth/jwt.go's Claims.Org field and IssueJWT's org parameter are already unconditional (present regardless of -multitenant), so a JWT-only app needs no further wiring. -multitenant adds the rest — auth.ContextWithOrg/auth.Org(r) (a context key deliberately distinct from Subject), StartSession/SessionFromRequest gaining an org parameter/return value (only when -auth includes session), and core.User.OrgId (only when a core database connection and a JWT-capable -auth choice are both present — the same eligibility core/users.go itself already has). Whichever credential succeeds, middleware.RequireAuth attaches Org alongside Subject: a JWT's claims.Org, or a session's stored org. A service-key-authenticated request (X-Api-Secret) gets an Org only when it sends X-Org-Id and its service record allows acting for orgs (acts_for_orgs) — see "A service calling on someone's behalf" above.

Usage — same login handler as above, now threading org through:

// -auth jwt or both
token, err := auth.IssueJWT(userID, name, org, userType, userRole, 24*time.Hour)
response.JSON(w, http.StatusOK, LoginResponse{Token: token})

// -auth session or both
auth.StartSession(w, userID, org, false)  // rememberMe if -remember-me was set
response.JSON(w, http.StatusOK, LoginResponse{})

// any protected handler downstream
userID, ok := auth.Subject(r)
org, ok := auth.Org(r)  // scope your own store/service queries to this

Retrofit an already-scaffolded app with nexler update -multitenant (a separate, explicit opt-in step — see nexler update).