Skip to content

Comments

Implement cross-device session persistence with D1 database#20

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/implement-session-history-db
Draft

Implement cross-device session persistence with D1 database#20
Copilot wants to merge 6 commits intomainfrom
copilot/implement-session-history-db

Conversation

Copy link
Contributor

Copilot AI commented Nov 9, 2025

Session history was stored only in localStorage, limiting access to a single browser. Users logging in from different devices couldn't access their conversation history.

Changes

Database Layer

  • Added Cloudflare D1 schema with users, chat_sessions, chat_messages tables
  • Created database helper functions in src/lib/server/db.ts for CRUD operations
  • User sessions scoped by GitHub user ID with foreign key constraints

API Layer

  • Implemented 6 RESTful endpoints under /api/sessions for session management
  • All endpoints require GitHub OAuth authentication
  • Type-safe request/response handling with proper error codes

Client Layer

  • Extended sessionStore with syncFromDatabase(), syncSessionToDatabase(), and deleteSessionFromDatabase() methods
  • localStorage maintained as client-side cache for performance
  • Background synchronization transparent to user

Security

  • Replaced Math.random() with crypto.randomUUID() for session ID generation
  • Prepared statements prevent SQL injection
  • All queries filter by authenticated user's GitHub ID

Example Usage

// Sessions automatically sync to database on creation
sessionStore.createSession('starspacegroup/apollo');

// Load sessions from database on login
await sessionStore.syncFromDatabase();

// Sessions available across all devices for the same GitHub account

Configuration

Local development:

npx wrangler d1 execute DB --local --file=./schema.sql

Production requires D1 database creation and wrangler.jsonc update with database ID.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • sparrow.cloudflare.com
    • Triggering command: /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/apollo/apollo/node_modules/wrangler/wrangler-dist/cli.js d1 execute DB --local --file=./schema.sql (dns block)
    • Triggering command: /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/apollo/apollo/node_modules/wrangler/wrangler-dist/cli.js d1 execute DB --local --command=SELECT name FROM sqlite_master WHERE type='table' ORDER BY name; (dns block)
  • workers.cloudflare.com
    • Triggering command: /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/apollo/apollo/node_modules/wrangler/wrangler-dist/cli.js d1 execute DB --local --file=./schema.sql (dns block)
    • Triggering command: /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/apollo/apollo/node_modules/wrangler/wrangler-dist/cli.js d1 execute DB --local --command=SELECT name FROM sqlite_master WHERE type='table' ORDER BY name; (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Consistent session history across devices using GitHub login</issue_title>
<issue_description>As a user
I want my session history to be consistent across all devices
So that I have a seamless experience regardless of where I log in

Acceptance Criteria

  • Given I log in with my GitHub account on one device, when I check my session history, then it should be recorded in a database
  • Given I log in with the same GitHub account on a second device, when I check my session history, then it should be identical to the history on the first device

Additional Details

  • Implement a database to store user sessions
  • Ensure data consistency and security across devices
  • Utilize the existing GitHub OAuth process for user authentication and data retrieval

Definition of Done

  • Database is implemented and integrated with GitHub logins
  • Session history is consistent across all devices for GitHub users
  • Security measures are in place to protect user data
  • Documentation is updated with details on how session history works</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 9, 2025
Co-authored-by: davis9001 <72961+davis9001@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 9, 2025

Deploying apollo with  Cloudflare Pages  Cloudflare Pages

Latest commit: f532bb1
Status: ✅  Deploy successful!
Preview URL: https://895ee32c.apollo-8vm.pages.dev
Branch Preview URL: https://copilot-implement-session-hi.apollo-8vm.pages.dev

View logs

Copilot AI and others added 4 commits November 9, 2025 15:05
Co-authored-by: davis9001 <72961+davis9001@users.noreply.github.com>
Co-authored-by: davis9001 <72961+davis9001@users.noreply.github.com>
Co-authored-by: davis9001 <72961+davis9001@users.noreply.github.com>
Co-authored-by: davis9001 <72961+davis9001@users.noreply.github.com>
Copilot AI changed the title [WIP] Add consistent session history across devices using GitHub login Implement cross-device session persistence with D1 database Nov 9, 2025
Copilot AI requested a review from davis9001 November 9, 2025 15:16
@davis9001 davis9001 moved this to Todo in Apollo Nov 28, 2025
@davis9001 davis9001 added this to Apollo Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Consistent session history across devices using GitHub login

2 participants