Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import {
SESSION_COOKIE,
} from './features/auth/sessionCookie.js';

/**
* Wires HTTP guards, session context and GraphQL resolvers into one handler.
* Injected auth/database/storage dependencies let tests exercise the same
* request flow without sharing production sessions, tables or image files.
*/
export function createApp(
service: AuthService = authService,
trustProxy = process.env.AUTH_TRUST_PROXY === 'true',
Expand Down Expand Up @@ -69,8 +74,13 @@ export function createApp(
limits,
),
);
// Resolvers schedule cookie changes through context.setSession; onResponse
// writes them to the matching HTTP response. Request keys keep concurrent
// login/logout responses separate without retaining completed requests.
const cookies = new WeakMap<Request, string>();
return createYoga<{ req?: IncomingMessage }, AuthContext>({
// The browser uses the same origin through Vite/Apache. Together with the
// custom header below, disabling CORS prevents cross-origin operation calls.
cors: false,
graphiql: enableGraphiql && {
// POST requests without this header are rejected (CSRF check below).
Expand Down Expand Up @@ -124,6 +134,9 @@ export function createApp(
},
),
);
// /uploads is handled before GraphQL parsing: it accepts image bytes,
// not JSON, and applies its own size, CSRF and session/access checks.
// A null response means this request belongs to the GraphQL handler.
const imageResponse = await handleImageRequest(
request,
service,
Expand Down Expand Up @@ -185,6 +198,7 @@ export function createApp(
}
},
onResponse({ request, response }) {
// Includes both new sessions and cookie expiry after logout/invalid auth.
const cookie = cookies.get(request);
if (cookie) response.headers.append('Set-Cookie', cookie);
// GraphQL answers are per-user and never cached. Image responses
Expand All @@ -195,6 +209,9 @@ export function createApp(
},
],
context: async ({ request, req }) => {
// Resolve the cookie against server-side sessions for every operation.
// An invalid/expired session clears the cookie; a service error propagates
// instead, so a temporary failure does not silently log the user out.
const token = readSessionCookie(request);
const user = token ? await service.currentUser(token) : null;
if (
Expand All @@ -210,6 +227,8 @@ export function createApp(
),
user,
sessionToken: user ? token : null,
// Auth resolvers call this on login/register/logout. The token stays in
// the HttpOnly cookie rather than being returned as GraphQL response data.
setSession: (nextToken) => {
cookies.set(request, sessionCookie(nextToken, secureCookie));
context.sessionToken = nextToken;
Expand Down
Loading