Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

Authentication system

Introduction#

Extension auth: React hook, browser OAuth via identity, token refresh, logout, and session persistence with the backend.

Project structure#

The authentication system spans the extension’s side panel (React + browser APIs) and the backend (FastAPI). The key files are:

  • Hook for authentication state and token lifecycle
  • Login UI component
  • Application shell that renders the login screen or settings based on auth state
  • Settings UI that displays tokens and exposes manual refresh/logout actions
  • Backend API surface for token exchange and refresh

Core components#

  • useAuth hook
  • Initializes auth state from browser local storage
  • Detects token age and refreshes automatically when appropriate
  • Exposes login via browser.identity OAuth, GitHub demo login, logout, and manual refresh
  • Provides helpers to compute token age and expiry
  • SignInScreen component
  • Renders two login options: Google OAuth and GitHub demo
  • Uses styled buttons and animations for UX
  • App shell
  • Renders SignInScreen when unauthenticated or UnifiedSettingsMenu when authenticated
  • Handles first-time setup redirection flag
  • Settings UI
  • Displays token info and exposes manual refresh and logout actions

Architecture overview#

The authentication flow integrates browser identity APIs, a backend service, and local storage. The diagram below maps the actual code paths.

Detailed component analysis#

useAuth hook#

The hook encapsulates:

  • Initialization from browser storage
  • Automatic token refresh based on token age
  • Manual refresh and logout
  • Token age/expiry computation
  • First-time setup flag handling

Key behaviors:

  • On mount, reads stored user data and triggers token refresh checks
  • Uses a threshold to decide whether to refresh the access token
  • Persists updated tokens back to storage and updates React state
  • Exposes handlers for Google OAuth login, GitHub demo login, logout, and manual refresh
  • Provides UI helpers to display token age and expiry

SignInScreen component#

The component renders:

  • A hero section with animated visuals
  • Two login buttons:
  • Continue with Google (OAuth)
  • Continue with GitHub (demo bypass)
  • Responsive styling and hover effects

App shell integration#

The App component:

  • Consumes useAuth to determine whether to render SignInScreen or UnifiedSettingsMenu
  • Handles first-time setup redirection based on a storage flag
  • Subscribes to storage changes to stay in sync with the hook

Settings UI (tokens and actions)#

The settings UI surfaces:

  • Access token visibility toggle
  • Refresh token visibility toggle (blurred)
  • Manual refresh button (when refresh token present)
  • Logout button

Dependency analysis#

  • Frontend-to-backend dependencies
  • useAuth.ts calls backend endpoints for token exchange and refresh
  • App.tsx depends on useAuth for rendering decisions
  • Settings components depend on useAuth for token display and actions
  • Backend routing
  • main.py registers routers under various prefixes
  • routers/github.py defines a GitHub endpoint used by services
  • services/github_service.py orchestrates GitHub-related operations

Performance considerations#

  • Token refresh threshold
  • The hook refreshes tokens before they reach a configured age threshold, reducing latency during requests
  • Local storage synchronization
  • Storage change subscriptions keep the UI in sync across sessions
  • UI responsiveness
  • Loading states and alerts provide feedback during long-running operations like OAuth and network requests

Troubleshooting guide#

Common issues and resolutions:

  • Authentication cancelled or denied
  • The hook detects cancellation/denial keywords and shows a plain alert
  • Backend service not running
  • Errors during token exchange or refresh trigger alerts instructing to verify backend availability
  • Token expired or refresh failed
  • The hook sets explicit status messages and advises re-authentication when refresh fails
  • Manual refresh unavailable
  • If no refresh token is present, the UI disables manual refresh and prompts re-login

Security considerations#

  • Token storage
  • Tokens are stored in browser local storage; consider encrypting sensitive fields for production
  • Token exposure
  • The settings UI supports toggling token visibility; use caution when sharing screens
  • Refresh token handling
  • Refresh tokens renew the session; keep transport and storage locked down
  • OAuth consent
  • The Google OAuth flow requests offline access and broad scopes; review and minimize scopes as needed

Conclusion#

OAuth in the browser, refresh on expiry, clear storage on logout. The hook is the only API the UI should talk to for session state.