Examples menu

Login Flow

Log in with one of the demo accounts to reach the dashboard and profile pages, then log out. Those two routes sit in a Via::group() behind a PSR-15 AuthMiddleware; the login form is public.

Three routes, one middleware. /examples/login is public. Dashboard and profile are protected via Via::group()->middleware(new AuthMiddleware(...)): one call shields both.
AuthMiddleware reads the session id from the request's via.session attribute, looks up sessionData('auth') in the server-side session store, and either redirects (302) or passes the auth record downstream as a request attribute. It runs again when a tab that was away comes back.
The handlers read $c->getRequestAttribute('auth'): the middleware-set attribute is automatically bridged from the PSR-7 request to the Via Context. No manual session checks needed.
Login and logout rotate the session cookie with $c->regenerateSession(), so a cookie someone planted or read before stops reaching the account 10 seconds later. Logout also clears sessionData('auth'), and the middleware blocks protected pages until the next login.

Signals

username string tab scope initial: empty string

Username input bound to the login form.

password string tab scope initial: empty string

Password input. Cleared on failure.

error string tab scope initial: empty string

Validation error message shown beneath the form.

Actions

login

Validates credentials. On success, rotates the session cookie, writes auth to sessionData and redirects to the middleware-protected dashboard.

Views

login.html.twig

Login form. Redirects to dashboard if already authenticated.

login_dashboard.html.twig

Protected dashboard. Auth data injected by AuthMiddleware via request attributes.

login_profile.html.twig

Protected profile page. Same AuthMiddleware protects it via the shared group.

Middleware

AuthMiddleware

PSR-15 middleware that checks sessionData('auth') and redirects unauthenticated requests to the login form. As route middleware it runs on page loads and when a tab that was away comes back, not on actions or SSE connects: a dashboard tab left open at logout keeps its stream until it reloads.