backend/main.py- FastAPI application entry point with CORS configurationbackend/config.py- Environment settings and configuration managementbackend/models.py- Pydantic data models for requests/responsesbackend/requirements.txt- Python dependencies
-
backend/services/circle_service.py- Circle Headless API integrationget_auth_token()- Generate auth token for user by emailverify_member()- Verify Circle member with access tokenget_member_by_email()- Fetch member details by emailrefresh_token()- Refresh Circle access token
-
backend/services/auth_service.py- JWT and Google OAuth managementcreate_access_token()- Create JWT tokenverify_token()- Verify JWT tokenverify_google_token()- Verify Google OAuth token
backend/routers/auth.py- Authentication endpointsPOST /auth/login- Email/password loginPOST /auth/google- Google OAuth loginGET /auth/me- Get current userPOST /auth/refresh- Refresh token
backend/middleware/auth_middleware.py- Token verification middleware
backend/.env.example- Environment variables templatebackend/.gitignore- Git ignore rules for Python
src/islands/LoginForm.tsx- Login form component with:- Email/password login form
- Google OAuth button integration
- User session display
- Auto-login on page load
- Token management
src/pages/login.astro- Login page with complete UI
src/utils/auth.ts- Authentication service utilityAuthService.login()- Login with email/passwordAuthService.loginWithGoogle()- Login with GoogleAuthService.checkAuth()- Check authentication statusAuthService.logout()- Logout userAuthService.refreshToken()- Refresh token- Global state management with signals
src/types/google-signin.d.ts- TypeScript definitions for Google Sign-In
backend/README.md- Backend setup and API documentationCIRCLE_AUTH_SETUP.md- Complete setup guidestart-auth-servers.sh- Startup script for both servers
.gitignore- Updated to ignore backend files
- β Email/Password login (verifies Circle membership)
- β Google OAuth login
- β JWT token-based authentication
- β Token refresh mechanism
- β Auto-login on page load
- β JWT tokens with configurable expiration (7 days default)
- β CORS protection
- β Secure token storage (localStorage)
- β Circle membership verification
- β Google OAuth integration
- β Fetch user profile from Circle
- β Display user info (name, email, avatar)
- β Logout functionality
- β Session persistence
POST /auth/login- Login with email/passwordPOST /auth/google- Login with Google OAuthGET /auth/me- Get current authenticated userPOST /auth/refresh- Refresh Circle access token
GET /- API infoGET /health- Health check
cd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Edit .env with your credentials
python main.py# From project root
npm install
npm run devchmod +x start-auth-servers.sh
./start-auth-servers.sh- Go to Circle admin β Settings β Developers β Tokens
- Create "Headless Auth" token
- Add to
backend/.env:CIRCLE_HEADLESS_TOKEN=your_token_here CIRCLE_COMMUNITY_ID=your_community_id
Create backend/.env with:
CIRCLE_HEADLESS_TOKEN- Your Circle API tokenCIRCLE_COMMUNITY_ID- Your Circle community IDGOOGLE_CLIENT_ID- Google OAuth client ID (already provided)GOOGLE_CLIENT_SECRET- Google OAuth secret (already provided)JWT_SECRET- Strong secret key (generate your own!)FRONTEND_URL- Frontend URL (http://localhost:4321)
ai_builders_tutorial/
βββ backend/ # Python FastAPI backend
β βββ main.py # FastAPI app
β βββ config.py # Settings
β βββ models.py # Data models
β βββ requirements.txt # Dependencies
β βββ .env.example # Environment template
β βββ .gitignore # Git ignore
β βββ README.md # Backend docs
β βββ services/
β β βββ circle_service.py # Circle API
β β βββ auth_service.py # JWT & 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 types
β βββ utils/
β βββ auth.ts # Auth service
β
βββ CIRCLE_AUTH_SETUP.md # Setup guide
βββ start-auth-servers.sh # Startup script
βββ .gitignore # Updated git ignore
# Health check
curl http://localhost:8000/health
# Login
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "any"}'
# Get user
curl http://localhost:8000/auth/me \
-H "Authorization: Bearer YOUR_TOKEN"- Visit: http://localhost:4321/login
- Try email login (must be Circle member)
- Try Google login (must be Circle member)
- Verify user info displays
- Test logout
# Circle API
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
GOOGLE_CLIENT_ID=695004012662-a3981egieh12pqcbb57sbiug99b48mos.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-Z57roRadsZ74hWhr0U-Jl3TP_OG
# JWT
JWT_SECRET=your_super_secret_jwt_key_change_this
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=10080 # 7 days
# CORS
FRONTEND_URL=http://localhost:4321- β
Create
backend/.envfrom.env.example - β
Get Circle API token and add to
.env - β Generate strong JWT_SECRET
- β Start servers and test login
- Email verification
- Password reset flow
- Role-based access control
- User profile management
- Activity logging
- Rate limiting
- Refresh token rotation
- Remember me functionality
- Multi-factor authentication
- β
Environment variables in
.gitignore - β CORS protection configured
- β JWT tokens with expiration
- β Secure token storage
β οΈ ChangeJWT_SECRETin productionβ οΈ Use HTTPS in productionβ οΈ UpdateFRONTEND_URLfor production
- Check Python 3.8+ is installed
- Activate virtual environment:
source venv/bin/activate - Install dependencies:
pip install -r requirements.txt - Verify
.envfile exists
- Verify token has "Headless Auth" permission
- Check community ID is correct
- Test token in Circle dashboard
- Check
FRONTEND_URLin.envmatches frontend - Verify CORS middleware in
main.py
- Check Google Client ID
- Verify script is loaded (browser console)
- Check for JavaScript errors
- Circle API Documentation
- FastAPI Documentation
- Google Sign-In Documentation
- JWT.io
- Preact Documentation
- Astro Documentation
import { AuthService, currentUser, isAuthenticated } from '../utils/auth';
// Check if user is logged in
const checkAuth = async () => {
const isAuth = await AuthService.checkAuth();
if (!isAuth) {
window.location.href = '/login';
}
};
// Login
const login = async (email: string, password: string) => {
const result = await AuthService.login(email, password);
if (result.success) {
// User is logged in, currentUser.value is set
console.log('Welcome', currentUser.value?.name);
} else {
console.error(result.error);
}
};
// Logout
const logout = () => {
AuthService.logout();
window.location.href = '/login';
};---
import { AuthService } from '../utils/auth';
// Server-side auth check
const authHeader = Astro.request.headers.get('Authorization');
if (!authHeader) {
return Astro.redirect('/login');
}
---β
Backend server starts successfully
β
Frontend server starts successfully
β
Can visit /login page
β
Can login with Circle member email
β
Can login with Google (Circle member)
β
User info displays after login
β
Logout works correctly
β
Session persists on page reload
β
API docs accessible at /docs
For issues or questions:
- Check documentation in
CIRCLE_AUTH_SETUP.md - Review backend logs
- Check browser console
- Contact: support@theaibuilders.dev