Session middleware
attachSession() resolves the session for every request and decorates req.authdog with the token, user, and isAuthenticated flag.
- req.authdog per request
- Token, user, and flag
Express
Protect your Node.js backend with session middleware, a requireAuth gate, and a logout handler. Key parsing, cookies, and the trusted-host allowlist are handled for you.
Attach the session
// server.ts
import { createAuthdog } from "@authdog/express"
const authdog = createAuthdog({
publicKey: process.env.PK_AUTHDOG!,
})
app.use(authdog.attachSession())
Gate routes with requireAuth
// routes.ts
app.get("/me", authdog.requireAuth, (req, res) => {
res.json(req.authdog.user)
})
app.get("/logout", authdog.logout)
Node backend native
Session middleware, an auth gate, and a logout handler that drop into any Express 4 or 5 app.
attachSession() resolves the session for every request and decorates req.authdog with the token, user, and isAuthenticated flag.
Protect any route with the requireAuth guard: the real enforcement point that rejects requests without a valid, verified session.
The public key is parsed once at startup and bearer tokens are only ever sent to a trusted, https identity host, enforced for you.
attachSession({ fetchUser: false }) skips the per-request user lookup for high-throughput services that validate tokens elsewhere.
A built-in logout handler clears the session cookie and redirects to a sanitized target. No session teardown to write yourself.
Public-key parsing, cookie handling, and the trusted-host allowlist are shared with the rest of the Authdog Web SDK, and fully typed.
Add @authdog/express and your environment's public key.
app.use(authdog.attachSession()) once, at the top.
Add requireAuth to the routes you protect.
Install the SDK, attach the session middleware, and gate your routes with requireAuth today. Free to start, with secure defaults built in.