Skip to content

[Feature]: First-party session, auth, and middleware primitives for server #132

Description

@JosunLP

Pre-flight checklist

  • I searched existing issues and did not find a duplicate request.
  • I explained the problem or use case, not just the desired API.

Problem or use case
Beyond WebSocket sessions, server has no first-party session, authentication, or composable middleware story. Every non-trivial service needs request-scoped sessions, some auth primitive, and a middleware pipeline; without them, adopters must hand-roll these or pull dependencies — which both undermines the "batteries-included, dependency-free backend" claim and is a blocker to server Stable (#131).

Proposed solution
Add a small, secure-by-default middleware pipeline plus session and auth primitives: signed/encrypted cookie sessions, a pluggable session store interface (in-memory default, bring-your-own for Redis/etc. without bundling a client), CSRF protection that composes with the security module, and minimal auth helpers (credential verification hooks, route guards mirroring the router's guard ergonomics). Keep each primitive independently importable and tree-shakeable.

Possible API or UX shape

import { createApp, session, csrf, guard } from "@bquery/bquery/server";

const app = createApp();
app.use(session({ secret: env.SECRET, store: memoryStore() }));
app.use(csrf());

app.route("GET", "/me", guard(ctx => !!ctx.session.userId), ctx => {
  return ctx.renderResponse(MeView, { userId: ctx.session.userId });
});

Alternatives considered
Documenting recipes for third-party session/auth libraries — contradicts the dependency-free goal and leaves security-critical code to users. A full identity provider — over-scoped; the right scope is primitives (sessions, CSRF, guards) that compose, not a turnkey auth system.

Relevant area
server

Additional context
Should reuse the existing security module (sanitize/Trusted Types/CSP) so the backend inherits secure-by-default behavior. Pairs naturally with form actions (#140) and file-based route actions (#149), which need server-side session context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions