Skip to content

Commit c3310d2

Browse files
feat(server): read device token cookie and pass to GraphQL context
- Read constructive_device_token cookie in auth middleware - Attach to req.deviceToken for downstream access - Pass as jwt.claims.device_token to DB procedures - Enables trusted device recognition in sign-in flows Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b1190bc commit c3310d2

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

graphql/server/src/middleware/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const isDev = () => getNodeEnv() === 'development';
1212
/** Default cookie name for session tokens. */
1313
const SESSION_COOKIE_NAME = 'constructive_session';
1414

15+
/** Cookie name for trusted device tracking. */
16+
const DEVICE_TOKEN_COOKIE_NAME = 'constructive_device_token';
17+
1518
/**
1619
* Extract a named cookie value from the raw Cookie header.
1720
* Avoids pulling in cookie-parser as a dependency.
@@ -143,6 +146,13 @@ export const createAuthenticateMiddleware = (
143146
);
144147
}
145148

149+
// Read device token cookie for trusted device tracking
150+
const deviceToken = parseCookieToken(req, DEVICE_TOKEN_COOKIE_NAME);
151+
if (deviceToken) {
152+
req.deviceToken = deviceToken;
153+
log.info('[auth] Device token cookie present');
154+
}
155+
146156
next();
147157
};
148158
};

graphql/server/src/middleware/graphile.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ const buildPreset = (
245245
if (req.get('User-Agent')) {
246246
context['jwt.claims.user_agent'] = req.get('User-Agent') as string;
247247
}
248+
if (req.deviceToken) {
249+
context['jwt.claims.device_token'] = req.deviceToken;
250+
}
248251

249252
if (req.token?.user_id) {
250253
const pgSettings: Record<string, string> = {

graphql/server/src/middleware/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ declare global {
1818
databaseId?: string;
1919
requestId?: string;
2020
token?: ConstructiveAPIToken;
21+
/** Device token from constructive_device_token cookie for trusted device tracking */
22+
deviceToken?: string;
2123
}
2224
}
2325
}

0 commit comments

Comments
 (0)