Complete setup guide for implementing Circle authentication in the AI Builders Tutorial platform.
This implementation provides:
- Python FastAPI backend with Circle Headless API integration
- React (Preact) frontend login component
- Email/password authentication (verifies Circle membership)
- Google OAuth authentication
- JWT token management
- Protected routes and user sessions
- Python 3.8+
- Node.js 18+
- Circle Business plan or higher
- Circle community:
community.theaibuilders.dev
# Navigate to backend directory
cd backend
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env with your credentials- Go to Circle admin: https://app.circle.so
- Navigate to: Settings β Developers β Tokens
- Click "Create API Token"
- Select "Headless Auth" permission
- Name it: "Custom Web App Auth"
- Copy the token and add to
.env:CIRCLE_HEADLESS_TOKEN=your_token_here CIRCLE_COMMUNITY_ID=your_community_id
# From backend directory
python main.py
# Or using uvicorn
uvicorn main:app --reloadBackend will run at: http://localhost:8000
# From project root
npm install # If needed
npm run devFrontend will run at: http://localhost:4321
Visit: http://localhost:4321/login
Try logging in with:
- Email/password (any password works, only Circle membership is verified)
- Google OAuth (must be a Circle community member)
ai_builders_tutorial/
βββ backend/
β βββ main.py # FastAPI app entry point
β βββ config.py # Environment configuration
β βββ models.py # Pydantic data models
β βββ requirements.txt # Python dependencies
β βββ .env # Environment variables (create this)
β βββ .env.example # Environment template
β βββ services/
β β βββ circle_service.py # Circle API integration
β β βββ auth_service.py # JWT & Google OAuth
β βββ routers/
β β βββ auth.py # Auth endpoints
β βββ middleware/
β βββ auth_middleware.py # Token verification
β
βββ src/
βββ islands/
β βββ LoginForm.tsx # Login component
βββ pages/
β βββ login.astro # Login page
βββ types/
βββ google-signin.d.ts # TypeScript definitions
Create backend/.env:
# Circle API Configuration
CIRCLE_HEADLESS_TOKEN=your_headless_auth_token_here
CIRCLE_COMMUNITY_ID=your_community_id
CIRCLE_API_URL=https://app.circle.so/api/v1
# Google OAuth Configuration
GOOGLE_CLIENT_ID=695004012662-a3981egieh12pqcbb57sbiug99b48mos.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-Z57roRadsZ74hWhr0U-Jl3TP_OG
# JWT Configuration
JWT_SECRET=your_super_secret_jwt_key_change_this_in_production
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=10080 # 7 days
# CORS Configuration
FRONTEND_URL=http://localhost:4321-
JWT_SECRET: Generate a strong random secret:
python -c "import secrets; print(secrets.token_urlsafe(32))" -
Never commit
.env: Already in.gitignore -
Production settings:
- Use HTTPS
- Update FRONTEND_URL to your production domain
- Use environment-specific secrets
Email/password login (verifies Circle membership)
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "any_password"
}'Response:
{
"access_token": "eyJhbG...",
"token_type": "bearer"
}Google OAuth login
curl -X POST http://localhost:8000/auth/google \
-H "Content-Type: application/json" \
-d '{
"credential": "google_credential_token"
}'Get current user (requires authentication)
curl http://localhost:8000/auth/me \
-H "Authorization: Bearer YOUR_TOKEN"Response:
{
"id": 12345,
"email": "user@example.com",
"name": "John Doe",
"avatar_url": "https://..."
}Refresh Circle access token
curl -X POST http://localhost:8000/auth/refresh \
-H "Content-Type: application/json" \
-d '{
"refresh_token": "circle_refresh_token"
}'curl http://localhost:8000/healthThe LoginForm component handles:
- Email/password login
- Google OAuth login
- User session management
- Token storage (localStorage)
- Auto-login on page load
// In your Astro component
const token = Astro.cookies.get('auth_token');
if (!token) {
return Astro.redirect('/login');
}
// Verify token with backend
const response = await fetch('http://localhost:8000/auth/me', {
headers: { 'Authorization': `Bearer ${token}` }
});
if (!response.ok) {
return Astro.redirect('/login');
}
const user = await response.json();curl http://localhost:8000/healthExpected: {"status": "healthy"}
- Visit http://localhost:4321/login
- Enter Circle member email
- Enter any password (membership is verified, not password)
- Click "Login with Email"
- Should see welcome message with user info
- Visit http://localhost:4321/login
- Click Google Sign-In button
- Sign in with Google account
- Must be a Circle community member
- Should see welcome message
# Get token from login response
TOKEN="your_token_here"
# Test /auth/me endpoint
curl http://localhost:8000/auth/me \
-H "Authorization: Bearer $TOKEN"Module Import Errors
# Ensure virtual environment is activated
source venv/bin/activate
pip install -r requirements.txtPort Already in Use
# Change port in main.py or use:
uvicorn main:app --reload --port 8001Circle API Errors
- Check your Circle API token is valid
- Verify Headless Auth permission is enabled
- Confirm community ID is correct
- Test API token in Circle dashboard
CORS Errors
- Check backend
FRONTEND_URLmatches frontend URL - Verify CORS middleware in
main.py - Check browser console for specific error
Google Sign-In Not Loading
- Check Google Client ID is correct
- Verify script is loaded (check Network tab)
- Check browser console for errors
Token Not Persisting
- Check localStorage in browser DevTools
- Verify token is being saved after login
- Check for browser privacy settings blocking localStorage
Docker (recommended):
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Environment Variables:
- Set all
.envvariables in your hosting platform - Use secrets management for sensitive data
Already configured with Astro - deploy as usual:
npm run buildUpdate FRONTEND_URL in backend .env to production URL.
- β Add email verification flow
- β Implement password reset
- β Add role-based access control
- β Create user profile management
- β Add activity logging
- β Implement rate limiting
- β Add refresh token rotation
For issues:
- Check backend logs:
tail -f backend/logs.txt - Check frontend console
- Review Circle API status
- Contact support@theaibuilders.dev
MIT License - See LICENSE file for details