页面简介
Build a Next.js wellbeing app that reads Fitbit data through Auth0 Token Vault and uses Fireworks to stream daily suggestions after users choose to share.
正文摘录
We'll build Daily Wellbeing, an app that reads your Fitbit activity and sleep data and uses Fireworks to suggest a few things to try today. You can compare the latest completed day with your recent averages and review the measurements behind the suggestions.Fitbit exposes these readings through the Google Health API v4. Access requires Google Cloud OAuth credentials and a custom Auth0 OAuth2 connection.Auth0 handles login, and Token Vault stores the Fitbit authorization credentials. After connecting Fitbit, you choose whether to share the summary with Fireworks. You can view your readings without running inference.What we'll buildSign users in with Auth0 and connect Fitbit after login.Retrieve a Fitbit token through Token Vault on the server.Calculate a daily report from activity, sleep, and resting heart-rate readings.Use a Fireworks model with a tool that returns the signed-in user's summary.Stream suggestions after the user chooses to share that summary.Suggestions cover general wellbeing, such as comfortable movement breaks and a consistent bedtime. The model prompt excludes diagnosis and treatment recommendations.Technology stackWe'll use Next.js 16 with TypeScript, the Auth0 Next.js SDK 4, and AI SDK 7 with its Fireworks provider. Zod validates requests and Fitbit responses. Dependency versions are pinned in the lockfile.The Auth0 Next.js SDK includes the Connected Accounts and Token Vault methods used here.PrerequisitesYou'll need:Node.js 22 or newer and npm. Use Node.js 24 LTS for a new setup.An Auth0 tenant with Token Vault, Connected Accounts, and the My Account API available.A personal Google account linked to a profile in the Google Health mobile app, recent health readings, and a Google Cloud Web OAuth client with Google Health API enabled. Google Workspace accounts cannot own the health profile.A Fireworks account, API key, credit, and a model that supports streaming and function calling.Get the sample application on GitHub.Set up the app with an AI coding agentGive your coding agent this prompt to clone the sample, configure the integrations, and run its checks. Auth0 provides login and Token Vault; the Fireworks provider comes from Vercel's AI SDK."Create a working Daily Wellbeing app using this repository as the starter: https://github.com/deepu105/fireworks-fitbit-coach Clone it into a new project directory. Preserve its tested server routes and pinned dependencies. Use @auth0/nextjs-auth0 4 for Auth0 login, Connected Accounts, and Token Vault. Use Vercel AI SDK 7 (the ai and @ai-sdk/react packages) with @ai-sdk/fireworks for Fireworks inference. The Fireworks provider is part of the AI SDK. 1. Read README.md, AGENTS.md, package.json, and .env.example in the sample. Install with npm ci. Copy .env.example to .env. Keep credentials out of source control, browser code, model messages, terminal output, and chat. Ask me to enter secrets in the environment file or provider console. 2. Follow the README's Auth0 CLI setup. Use a tenant with Token Vault, Connected Accounts, and the My Account API available. Inspect existing resources before creating any. Configure a Regular Web Application with Authorization Code, Refresh Token, and Token Vault grants. Match the sample's expiring non-rotating refresh tokens and My Account MRRT policy. Authorize user access with read:me:connected_accounts, create:me:connected_accounts, and delete:me:connected_accounts. Enable a login connection and bind the separate google-health connection to the app. 3. Follow https://developers.google.com/health/setup for Google Cloud setup. I need a personal Google account linked in the Google Health mobile app, Google Health API enabled, a Web OAuth client, and consent-screen test users when in Testing mode. Register https://YOUR_AUTH0_DOMAIN/login/callback with Google. Register http://localhost:3000/auth/callback with Auth0. Configure the custom google-health OAuth2 connection with the Google client credentials and scripts/google-health-profile.js. Enable its Connected Accounts purpose and request these three read scopes: https://www.googleapis.com/auth/googlehealth.activity_and_fitness.readonly https://www.googleapis.com/auth/googlehealth.sleep.readonly https://www.googleapis.com/auth/googlehealth.health_metrics_and_measurements.readonly 4. Complete AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET, AUTH0_SECRET, APP_BASE_URL, AUTH0_FITBIT_CONNECTION, REPORT_TIME_ZONE, FIREWORKS_API_KEY, and FIREWORKS_MODEL. Generate AUTH0_SECRET with openssl rand -hex 32. Keep the Fireworks key on the backend and Google OAuth credentials in Auth0. The sample uses accounts/fireworks/models/kimi-k3 with thinking disabled. Confirm model access and available credit for my account. 5. Preserve the flow: sign in with Auth0, connect Google Health, retrieve the current user's token through Token Vault, and show daily readings with seven-day personal comparisons. Keep missing values as unknown. Require an explicit sharing choice before calling Fireworks. Use the server-fetched report in getDailyFitbitSummary, then stream general wellbeing suggestions. Render Markdown without raw HTML or remote model-generated images. Keep identity, credentials, and API URLs outside model-controlled input. 6. Start npm run dev and leave development mode and live reload enabled. Help me sign in, connect my Google Health account, and view the report. Explain any missing setup or permissions. A new health account can have no readings; use synced data for a completed date. Let me review the readings and choose to share the summary before generating suggestions. Make external account or tenant changes only with the permissions I provide. Don't deploy or publish the app as part of this setup." Configure Google Health and Auth0Configure two OAuth callbacks: Google sends its authorization result to Auth0, and Auth0 redirects back to the Next.js app. Provider configuration Local development value Google Web OAuth client's redirect URI https://YOUR_AUTH0_DOMAIN/login/callback Auth0 application's Allowed Callback URLs http://localhost:3000/auth/callback Auth0 application's Allowed Logout URLs http://localhost:3000 Auth0 application's Allowed Web Origins http://localhost:3000 Use the same Auth0 domain throughout, including your custom domain if you have one.Configure the Google account and OAuth clientFollow the Google Health setup guide:Open the Google Health mobile app on your phone, sign in with a personal Google account, and finish creating or linking its health profile.Enable Google Health API in your Google Cloud project. Configure the OAuth consent screen and create a Web application OAuth client.Set its redirect URI to the Auth0 /login/callback URL above. Replace the guide's example https://www.google.com redirect with this callback.For an External/Testing OAuth app, add the personal Google account under Audience > Test users.Add the three read scopes in the next section under Data Access.Keep the Google OAuth client ID and secret for the Auth0 connection. Google Workspace accounts can sign into the app through Auth0, but Google requires a personal account for the health profile. You can use different accounts for these steps.Google provides no prefilled health-data sandbox. Sync your tracker or add a sleep log for yesterday in the mobile app. Google OAuth can authenticate an account without a linked health profile. The connection's profile script checks for that profile through the Health API.Configure the Auth0 applicationIn the Auth0 Dashboard, create a Regular Web Application and set the callback, logout, and origin values above. Enable a database or social connection for login. Users connect Fitbit after signing in.Under Advanced Settings > Grant Types, enable Authorization Code, Refresh Token, and Token Vault. Our tested setup uses non-rotating, expiring refresh tokens. Disable Allow Refresh Token Rotation to match it. The CLI payload sets a 30-day lifetime and seven-day idle lifetime. See Configure Token Vault for the grant configuration.Configure the My Account API using the third-party API quickstart:Under Applications > APIs, activate Auth0 My Account API if it isn't active.Under Application Access, authorize the application for user access with read:me:connected_accounts, create:me:connected_accounts, and delete:me:connected_accounts.Enable Allow Skipping User Consent for this first-party application as described in the quickstart.In the application's Multi-Resource Refresh Token settings, enable the policy for the My Account API and those Connected Accounts scopes.To configure the tenant from your terminal, follow the full Auth0 CLI setup in the sample README. It includes app creation, My Account API settings, the user grant, MRRT policy, connection creation, and application bindings. Dashboard labels can vary with tenant availability.Add the Google Health connectionCreate a Custom OAuth2 connection named google-health, using these values: Setting Value Client ID and Secret The Google Cloud Web OAuth credentials Authorization URL https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent Token URL https://oauth2.googleapis.com/token Purpose Connected Accounts for Token Vault Use these Google Health read scopes, separated by spaces:https://www.googleapis.com/auth/googlehealth.activity_and_fitness.readonlyhttps://www.googleapis.com/auth/googlehealth.sleep.readonlyhttps://www.googleapis.com/auth/googlehealth.health_metrics_and_measurements.readonlyThese scopes grant read access to activity, sleep, and health measurements. Users can grant some of them; the report includes the measurements they permit.Paste this Fetch User Profile Script into the connection:// Paste this function into the custom OAuth2 connection's profile script. function fetchUserProfile(accessToken, ctx, cb) { request.get({ url: 'https://health.googleapis.com/v4/users/me/identity', headers: { Authorization: 'Bearer ' + accessToken }, json: true, timeout: 10000 }, function (er