You are extending an existing Google Earth inspired web app called TerraView, built with Next.js (App Router), TypeScript, Tailwind CSS, and a 3D globe library.
The base application already implements:
- A full 3D globe viewer with camera controls
- Search via OpenStreetMap Nominatim
- Pins, saved places, layers, and measurement tools
- Clean app architecture with Zustand, dynamic imports, and localStorage persistence
Your task is to add authentication and PostHog analytics integration, using Composio to connect and manage PostHog. This is not optional or conceptual. It must be implemented end to end with real code.
We want to:
- Authenticate users
- Track user activity with PostHog
- Capture and store user location data
- Visually display active users on the globe in real time or near real time
When someone opens TerraView:
- They authenticate
- Their session is tracked in PostHog
- Their approximate location is captured
- A visible marker representing that user appears on the globe UI
This should feel native to the product, not bolted on.
Implement authentication with a clean, production friendly approach.
Requirements:
- Use a standard auth solution compatible with Next.js App Router
- Support at least email based login
- Auth state must be available client side and server side
- Protect analytics and user location tracking behind authentication
- Persist user session across reloads
Expose:
user.iduser.emailor equivalent identifier
This identifier must be used consistently for PostHog tracking.
Integrate PostHog for analytics and user tracking.
Use Composio to set up and manage the PostHog connection. Do not bypass Composio. The model must rely on Composio tooling to interact with PostHog.
You should review and use the following Composio documentation to understand how tools and providers work:
- https://docs.composio.dev/docs/quickstart
- https://docs.composio.dev/docs/providers/openai
- https://docs.composio.dev/docs/toolkits/fetching-tools-and-toolkits
PostHog requirements:
-
Initialize PostHog once per authenticated session
-
Identify users using their authenticated user ID
-
Track core events such as:
- App loaded
- User logged in
- Globe interaction
- Location update
-
Store user properties where appropriate
All PostHog keys must be loaded via environment variables.
We want to capture and visualize user locations.
Requirements:
- Capture approximate user location using browser APIs
- Fall back gracefully if location access is denied
- Send location data to PostHog as an event or user property
- Use latitude and longitude with reasonable precision
Do not aggressively poll location. Capture on:
- Initial load
- Meaningful movement or session refresh
This is critical.
On the TerraView globe:
- Display markers representing authenticated users
- These markers come from PostHog tracked data
- Each marker should represent a user location
UI requirements:
- User markers must be visually distinct from saved pins
- Hover or click reveals basic info such as user ID or anonymized label
- Markers update when location data changes
- The globe must remain performant with multiple users
You may assume a reasonable number of concurrent users.
The implementation should clearly show:
- How auth ties into PostHog identify
- How location events are sent to PostHog via Composio
- How location data is queried or retrieved for display
- How client and server responsibilities are separated
If polling or periodic refresh is required, implement it cleanly and efficiently.
You must:
- Follow the existing app structure
- Add new files only where appropriate
- Keep concerns separated
- Use strong TypeScript typing everywhere
- Avoid hacks or brittle shortcuts
Include:
lib/utilities for PostHog and Composio- Clear initialization logic
- Graceful error handling
- Comments where logic is non obvious
When you respond, you must output:
- A clear explanation of how authentication, Composio, and PostHog are wired together
- New and modified files with full code blocks
- Environment variables required
- Any PostHog event schemas used
- Notes on limitations and next improvements
Do not use pseudocode for core logic. This must be runnable.
The final app should still run with:
npm installnpm run dev