FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
stevekrouse

stevekrouse

github-oauth-template

Public
Like
github-oauth-template
Home
Code
4
backend
3
frontend
2
shared
1
README.md
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
backend
/
README.md
Code
/
backend
/
README.md
Search
5/23/2025
README.md

Backend - GitHub OAuth Template

This directory contains the backend implementation for the GitHub OAuth template using Hono framework.

Files

  • index.ts - Main Hono application with OAuth routes and API endpoints
  • github.ts - GitHub API client for server-side requests

Features

OAuth Flow

  • Login: /auth/login - Initiates GitHub OAuth flow
  • Callback: /auth/callback - Handles OAuth callback and token exchange
  • Logout: /auth/logout - Clears authentication cookies

API Endpoints

  • User Profile: GET /api/user - Returns authenticated user's GitHub profile
  • Repositories: GET /api/repos - Returns user's repositories
  • Health Check: GET /health - Application health status

Security Features

  • HTTP-only Cookies: Tokens stored securely, not accessible to JavaScript
  • CSRF Protection: State parameter validation during OAuth flow
  • Secure Cookies: HTTPS-only transmission with SameSite protection
  • Token Validation: Automatic cleanup of invalid tokens

Cookie Management

The application uses custom cookie helper functions since Hono's cookie helpers had import issues:

// Get cookie value const token = getCookie(c, "github_token"); // Set secure cookie setCookie(c, "github_token", accessToken, { httpOnly: true, secure: true, sameSite: "Lax", maxAge: 60 * 60 * 24 * 30 // 30 days }); // Delete cookie deleteCookie(c, "github_token");

Environment Variables

Required environment variables:

  • GITHUB_CLIENT_ID - GitHub OAuth app client ID
  • GITHUB_CLIENT_SECRET - GitHub OAuth app client secret

Error Handling

The application includes comprehensive error handling:

  • OAuth errors are displayed with user-friendly messages
  • Invalid tokens are automatically cleared
  • API errors return appropriate HTTP status codes
  • All errors bubble up with full context for debugging

GitHub API Integration

The GitHubAPI class provides a clean interface for GitHub API requests:

const github = new GitHubAPI(accessToken); const user = await github.getUser(); const repos = await github.getRepos();

All requests include proper headers and error handling.

FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
ExploreDocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareersBrandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.