Skip to content

Session ​

Keep users signed in across browser requests with Auth. GoForj Auth stores session records in your database and sends credentials through HttpOnly cookies. Protect routes with authService.RequireAuth in app/routes.go.

The session row controls expiry and revocation. GoForj checks it when a request presents the authentication cookie. Use the database for application state such as a checkout and cookies for small browser preferences.

API-oriented apps and the Vue and React starters mount JSON Auth routes under /api/v1/auth. The templ + htmx starter uses browser routes such as /login and /logout with the same Auth service. Use the routes your app actually mounts.

Session Flow ​

The access token contains user and session identity. It does not replace the persisted session check.

The refresh cookie contains sessionID.secret. Only a hash of the secret is stored.

Expiry Policy ​

Default policy:

SettingDefaultMeaning
AUTH_ACCESS_TOKEN_TTL15mAccess JWT lifetime.
AUTH_SESSION_IDLE_TTL2hMaximum session inactivity.
AUTH_SESSION_TTL24hNormal absolute session lifetime.
AUTH_REMEMBER_SESSION_TTL720hRemembered absolute session lifetime.

The earliest applicable expiry wins. A valid access JWT cannot revive an expired, revoked, or idle session.

Refresh Behavior ​

Protected middleware can recover from a missing or expired access cookie using the current refresh secret. That recovery reissues access state without rotating the refresh secret.

Explicit POST /api/v1/auth/refresh rotates the refresh secret and updates refresh_rotated_at.

Keeping normal middleware recovery separate from explicit rotation prevents concurrent browser requests at the access-token boundary from repeatedly invalidating each other.

Revocation ​

Generated routes support:

  • current-session logout
  • logout from all sessions
  • session listing
  • revoking one owned session
  • revoking other sessions after a password change
  • revoking sessions after a password reset

Logout and current-session revoke own cookie clearing. An unrelated protected-request 401 does not clear cookies because it may race with newer valid browser state.

Generated auth cookies use:

  • HttpOnly: true
  • SameSite: Lax
  • Secure from AUTH_COOKIE_SECURE

Use AUTH_COOKIE_SECURE=true behind production HTTPS. auto is useful when the same App must support plain-HTTP local development and HTTPS deployment.

Generated auth cookies remain host-scoped. This keeps sibling hosts outside the authentication boundary.

CSRF Boundary ​

Cookie-authenticated requests need CSRF protection on state-changing routes, including login and logout. SameSite=Lax is an additional defense. CORS alone does not stop cross-site form submissions. Keep GET handlers free of mutations.

Auth and the starter UI do not automatically apply token-based CSRF middleware. Add the policy at route composition and update browser clients to send the token.

Client Rules ​

Browser clients should:

  • send requests with cookie credentials when crossing origins intentionally
  • treat 401 as an authentication result, not a reason to mutate cookies directly
  • avoid storing access or refresh values in local storage
  • use session-list and revoke routes for user-visible session controls

Verify Session Policy ​

Generated Auth includes integration coverage for cookie flags, refresh rotation, replay rejection, expiry, logout, logout-all, session listing, and per-session revocation. Run the focused session and cookie tests after changing auth configuration or middleware:

bash
go test -tags integration ./internal/auth -run \
  'Test(LoginSetsSecureCookieDefaults|ControllerLogoutAllRevokesEverySession|ControllerRevokeSessionRevokesOnlyRequestedOwnedSession|RefreshRejectsReplayedRefreshToken)Integration$' \
  -count=1

Expected result: all four tests pass against the App's test database.

After deployment, use a non-privileged test account to verify login, one protected request, explicit refresh, session listing, and logout. Inspect the response cookies in browser developer tools: both auth cookies remain HttpOnly, production HTTPS cookies are Secure, and logout expires the current credentials. Confirm a request made with the revoked session receives 401; a 200 from the public health endpoint does not prove session revocation works.

Monitor authentication failures and session cleanup through bounded metrics, logs, and Inspects without recording JWTs, refresh secrets, cookie headers, or password material.

Common Mistakes ​

Common mistakes

  • Do not trust a JWT after its session row is revoked or expired.
  • Do not persist raw refresh secrets.
  • Do not rotate refresh secrets from every concurrent protected request.
  • Do not clear cookies from arbitrary middleware failures.
  • Do not rely on CORS as CSRF protection.

Next Steps ​

  • Auth covers login setup and the JSON route contract.
  • Cookies covers reading, writing, and clearing application cookies.
  • CSRF protection adds token checks to browser routes.
  • OAuth explains provider-backed login and linking.
  • Production Hardening covers deployment policy.
  • Middleware explains HTTP middleware placement.