feat(core): register device metadata - #241
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
📝 WalkthroughWalkthroughSession creation now receives requests, derives device metadata and fingerprints, creates device-linked stateful sessions, and updates OAuth cookie handling. Authentication tests and OAuth integration tests verify the new device associations and session cookie flow. ChangesStateful session and device integration
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Request
participant OAuthCallback
participant createDevice
participant DevicesAdapter
participant StatefulSessionStorage
participant SessionCookie
Request->>OAuthCallback: OAuth callback request
OAuthCallback->>createDevice: Extract request device data
createDevice->>DevicesAdapter: Create device record
OAuthCallback->>StatefulSessionStorage: Create device-linked session
OAuthCallback->>SessionCookie: Set session token hash and CSRF cookie
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/session/stateful.ts`:
- Around line 1346-1356: Update the OAuth session creation flow around
createHash and config.adapter.createSession to retain the raw session token
separately, store only its hash in tokenHash, and return/set the raw token as
the cookie value. Ensure getSessionByToken receives the unhashed token, matching
the credential flow behavior.
- Around line 45-62: Update createDevice to call
config.adapter.getDeviceByFingerprint after generating the fingerprint, scoped
to the user. If a device exists, update its lastSeenAt and lastIp and return it;
otherwise retain the existing creation flow with firstSeenAt initialized for the
new device.
- Around line 1319-1321: Update the createOAuthAccount branch to persist
refreshTokenExpiresAt using the same accessToken.refresh_token_expires_in
conversion as the existing-account update path, returning null when the provider
omits the expiry. Keep the existing-account behavior unchanged.
In `@packages/core/src/shared/utils.ts`:
- Around line 282-285: Reorder the user-agent checks in the device
classification logic so the TV pattern is evaluated before the generic mobile
pattern. Keep the existing tablet, mobile, and bot classifications unchanged
otherwise, ensuring Android TV user agents return "tv" instead of "mobile".
In `@packages/core/test/actions/callback/stateful.test.ts`:
- Line 336: Strengthen the new-user OAuth tests around the device creation
assertions near createDeviceMock and lines 419-435: use a user-specific fixture
or factory, assert createDevice received the expected userId and device fields,
and verify the persisted session is associated with that same user/device owner
instead of only checking invocation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 537c3056-b565-40c3-9f22-a715e862fdc1
📒 Files selected for processing (14)
packages/core/src/@types/adapter.tspackages/core/src/@types/session.tspackages/core/src/api/signInCredentials.tspackages/core/src/api/signUp.tspackages/core/src/session/stateful.tspackages/core/src/session/stateless.tspackages/core/src/shared/utils.tspackages/core/test/actions/callback/stateful.test.tspackages/core/test/actions/signIn/signInCredentials/stateful.test.tspackages/core/test/actions/signUp/stateful.test.tspackages/core/test/api/stateful/signInCredentials.test.tspackages/core/test/api/stateful/signUp.test.tspackages/core/test/presets.tspackages/elysia/test/stateful/index.test.ts
Description
This pull request adds automatic device metadata collection and registration for the Stateful session strategy.
During authentication flows such as credential sign-in, OAuth/OIDC sign-in, and sign-up, the library now captures device information from the incoming request and stores it in the
Devicetable. This information can be used to identify trusted devices, audit authentication activity, manage active sessions, and support future security features.The device metadata is inferred primarily from the request headers, which are normalized and mapped into a consistent structure before being persisted.
Captured Metadata
The following information is collected when available:
Request Headers
The following headers are used to infer device metadata:
user-agentsec-ch-ua-platformsec-ch-ua-mobilex-forwarded-forcf-connecting-ipx-real-ipx-client-ipip@coderabbitai ignore