npm run dev
A Next.js application with Google Fit API integration using Privy authentication for secure OAuth token handling.
- π Secure OAuth token management with Privy
- π Google Fit step count data visualization
- β‘ Vercel serverless function optimization
- π Automatic token refresh and caching
- π¨ Responsive UI with real-time status updates
- π¨ Comprehensive error handling
- Frontend: Next.js 15, React 19, TypeScript, Tailwind CSS
- Authentication: Privy (OAuth + wallet support)
- Backend: Next.js API Routes with Vercel serverless functions
- APIs: Google Fit REST API
- Privy Account: Sign up at privy.io and create an app
- Google Cloud Console: Enable Google Fit API and configure OAuth credentials
- Vercel Account: For deployment
- Clone the repository:
git clone <your-repo-url>
cd nextjs-capstone- Install dependencies:
npm install- Set up environment variables:
cp .env.example .env.localEdit .env.local with your actual credentials:
NEXT_PUBLIC_PRIVY_APP_ID=your-privy-app-id
PRIVY_APP_SECRET=your-privy-app-secret- Go to Google Cloud Console
- Create/select a project
- Enable the Google Fit API
- Configure OAuth consent screen
- Create OAuth 2.0 credentials
- Add your domain to authorized origins
- Go to your Privy Dashboard
- Navigate to Configuration > App settings
- Copy your App ID and App Secret
- Add them to
.env.local
npm run devOpen http://localhost:3000 in your browser.
Testing the API locally:
- API routes work during development
- Test Google Fit integration at
http://localhost:3000/api/fit - Check browser console for detailed logs
- Use browser dev tools to inspect network requests
npm run buildβ API routes work locally - you can test everything before deploying:
- Start development server:
npm run dev- Test the Google Fit API:
- Login with Google through Privy
- Check browser console for OAuth token capture
- Click "Get Step Data" to test the integration
- API calls go through
http://localhost:3000/api/fit
- Debug information available:
- Browser console shows detailed logs
- Development UI shows debug panels
- Network tab shows API requests
π Option 1: Automatic Deployment (Recommended)
- Push to GitHub:
git add .
git commit -m "Complete Google Fit integration"
git push origin main- Connect to Vercel:
- Go to vercel.com
- Click "Import Project"
- Connect your GitHub repository
- Vercel will automatically detect it's a Next.js app
-
Add Environment Variables:
- Go to your Vercel dashboard
- Navigate to your project β Settings β Environment Variables
- Add these variables:
NEXT_PUBLIC_PRIVY_APP_ID=your-privy-app-id PRIVY_APP_SECRET=your-privy-app-secret - Important: The
PRIVY_APP_SECRETshould be added as a secure environment variable (not visible in build logs)
-
Deploy:
- Vercel automatically builds and deploys
- Your app will be live at
your-project.vercel.app
π Option 2: Manual Deployment
- Install Vercel CLI:
pnpm add -g vercel- Login to Vercel:
vercel login- Deploy:
vercel --prod- Add Environment Variables:
vercel env add NEXT_PUBLIC_PRIVY_APP_ID
vercel env add PRIVY_APP_SECRET- Enter your actual Privy credentials when prompted
- The
PRIVY_APP_SECRETwill be stored securely as an environment secret
Required for both local and production:
-
Get your Privy credentials:
- Go to Privy Dashboard
- Sign in to your account
- Navigate to your app β Configuration β App settings
- Copy your App ID and App Secret
-
Local development (
.env.local):
NEXT_PUBLIC_PRIVY_APP_ID=your-app-id-here
PRIVY_APP_SECRET=your-app-secret-here- Vercel production:
- Option A (Recommended): Add variables in Vercel dashboard:
- Go to your project β Settings β Environment Variables
- Add
NEXT_PUBLIC_PRIVY_APP_IDandPRIVY_APP_SECRET - Mark
PRIVY_APP_SECRETas secure (hidden in logs)
- Option B: Use Vercel CLI:
vercel env add NEXT_PUBLIC_PRIVY_APP_ID vercel env add PRIVY_APP_SECRET
- Option A (Recommended): Add variables in Vercel dashboard:
.env.local to version control. Add it to .gitignore.
The vercel.json file is configured for optimal Next.js deployment with pnpm:
{
"buildCommand": "pnpm build",
"devCommand": "pnpm dev",
"installCommand": "pnpm install --frozen-lockfile",
"framework": "nextjs"
}Note: The project uses pnpm as the package manager. Vercel automatically detects and uses pnpm for installations.
-
Test the live app:
- Visit your deployed URL
- Login with Google
- Test Google Fit integration
-
Monitor logs:
- Vercel dashboard β Functions β Logs
- Check for any API errors
-
Update DNS (if needed):
- Point your domain to Vercel's nameservers
- Configure custom domain in Vercel dashboard
Common issues:
- Build failures: Check environment variables are set
- API errors: Verify Privy credentials are correct
- OAuth issues: Ensure Google OAuth is configured properly
- Secret not found: Make sure environment variables are added in Vercel dashboard
Check logs:
- Vercel dashboard β Deployments β View Logs
- Vercel dashboard β Functions β Logs (for API route errors)
- Browser console for frontend errors
- Network tab for API request details
Environment Variable Issues:
- Ensure
PRIVY_APP_SECRETis marked as "secure" in Vercel - Check that variable names match exactly (case-sensitive)
- Verify the values are correct (copy-paste from Privy dashboard)
Runtime Configuration Issues:
- Remove custom runtime specifications from
vercel.json - Next.js API routes are handled automatically by Vercel
- Only specify
framework: "nextjs"invercel.json
Package Manager Issues:
- Ensure all commands in
vercel.jsonusepnpminstead ofnpm - The
installCommandshould bepnpm install --frozen-lockfile - Vercel should automatically detect and use pnpm
- User logs in with Google via Privy
- Privy captures OAuth tokens (stored securely server-side)
- Frontend attempts direct Google Fit API call using captured tokens
- Fallback to server-side API route if direct call fails
- Step data is processed and returned with visualization
Frontend-Only Approach:
- Uses
useOAuthTokenshook to capture Google OAuth tokens - Makes direct Google Fit API calls from the browser
- Falls back to server-side API route if needed
- Includes comprehensive debugging and error handling
OAuth Token Access:
- The
useOAuthTokenshook should capture tokens during OAuth flow - If tokens aren't captured, try clicking "Refresh Google Auth"
- Direct API calls may face CORS restrictions in some browsers
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Frontend βββββΆβ Direct API Call βββββΆβ Google Fit API β
β (Next.js) β β (Browser) β β β
β β β β β β
β β’ useOAuthTokensβ β β’ OAuth Token β β β’ Step Data β
β β’ Error Handlingβ β β’ Fallback Route β β β’ Processing β
β β’ UI Feedback β β β’ Comprehensive β β β’ Visualization β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
- OAuth token capture: Uses Privy's secure
useOAuthTokenshook - Direct API calls: Minimal server involvement for testing
- Fallback mechanism: Server-side API route as backup
- Token refresh: Automatic reauthorization on expiration
- Error boundaries: Graceful failure handling
- Input validation: Prevents injection attacks
Method: POST
Request Body:
{
"privyAccessToken": "string",
"userId": "string"
}Response:
{
"success": true,
"data": {
"days": [
{
"date": "2024-01-01",
"steps": 8500
}
],
"total": 42500
},
"metadata": {
"daysCount": 5,
"dateRange": {
"start": "2024-01-01T00:00:00.000Z",
"end": "2024-01-05T23:59:59.999Z"
},
"cached": false
}
}Error Responses:
{
"error": "Error description",
"code": "ERROR_CODE",
"action": "suggested_action"
}-
"No Google OAuth token found"
- Ensure user logged in with Google
- Check OAuth scopes include fitness data
- Verify Privy app secret is correct
-
"Token expired"
- The system automatically attempts reauthorization
- If persistent, check Google OAuth settings
-
Build failures
- Ensure all environment variables are set
- Check Vercel function logs for detailed errors
The app includes debug panels (development mode only) that show:
- Authentication status
- Token availability
- API response details
- Error information
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
- Setup: Add
PRIVY_APP_SECRETto.env.local - Test Locally:
npm run devβ Login with Google β Check console logs - Deploy: Push to GitHub β Connect to Vercel β Add environment variables
- Verify: Test live app with Google Fit integration
This project is licensed under the MIT License.