Skip to content

Commit 3972e63

Browse files
committed
fixing Strava integration issues
1 parent cacc967 commit 3972e63

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

app/api/strava/activities/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export async function POST(req: Request) {
2121
return NextResponse.json({ riddenRoads });
2222
} catch (error: any) {
2323
console.error('Strava Fetch Error:', error);
24-
return NextResponse.json({ error: error.message }, { status: 500 });
24+
return NextResponse.json({
25+
error: error.message,
26+
trace: error.stack
27+
}, { status: 500 });
2528
}
2629
}

app/page.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export default function Home() {
7070
setStravaRoads(data.riddenRoads);
7171
} else if (data.error) {
7272
setStravaError(data.error);
73+
setError({ message: data.error, trace: data.trace });
7374
}
7475
})
7576
.catch(err => {
7677
console.error('Failed to fetch Strava roads:', err);
7778
setStravaError(err.message);
79+
setError({ message: `Strava Connection Failed: ${err.message}` });
7880
});
7981
}, [stravaCredentials]);
8082

@@ -682,6 +684,14 @@ ${route.map(pt => ` <trkpt lat="${pt[1]}" lon="${pt[0]}">${pt[2] !== undefi
682684
// No need to close, let them see the "Saved!" state
683685
}}
684686
/>
687+
688+
{error && (
689+
<ErrorDialog
690+
message={error.message}
691+
trace={error.trace}
692+
onClose={() => setError(null)}
693+
/>
694+
)}
685695
</div>
686696
</main>
687697
);

components/StravaSettingsDialog.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export function StravaSettingsDialog({ isOpen, onClose, onSave }: StravaSettings
8080
https://www.strava.com/oauth/authorize?client_id=YOUR_CLIENT_ID&amp;response_type=code&amp;redirect_uri=http://localhost&amp;approval_prompt=force&amp;scope=read,activity:read_all
8181
</div>
8282
</li>
83+
<li className="text-amber-800 font-semibold bg-amber-50 p-2 rounded border border-amber-100">
84+
<Check className="w-3 h-3 inline mr-1" />
85+
IMPORTANT: On the authorization page, you MUST check all the boxes (especially &quot;View data about your activities&quot; and &quot;View data about your private activities&quot;) or the app will not be able to fetch your data.
86+
</li>
8387
<li> Authorize the app, then you will be redirected to a localhost URL. Copy the <code>code</code> parameter from the URL.</li>
8488
<li> Run this command in your terminal (mac/linux) to get the refresh token:
8589
<div className="mt-2 p-2 bg-indigo-100/50 rounded font-mono text-[10px] break-all border border-indigo-200 overflow-x-auto whitespace-pre">

lib/strava.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ export interface StravaActivity {
88
}
99

1010
export async function getStravaAccessToken(creds?: { clientId?: string; clientSecret?: string; refreshToken?: string }) {
11-
const clientId = creds?.clientId || process.env.STRAVA_CLIENT_ID;
12-
const clientSecret = creds?.clientSecret || process.env.STRAVA_CLIENT_SECRET;
13-
const refreshToken = creds?.refreshToken || process.env.STRAVA_REFRESH_TOKEN;
11+
// Check if we received any non-empty credentials in the UI object
12+
const hasUICreds = !!(creds && (creds.clientId || creds.clientSecret || creds.refreshToken));
13+
14+
const clientId = (creds?.clientId) || process.env.STRAVA_CLIENT_ID;
15+
const clientSecret = (creds?.clientSecret) || process.env.STRAVA_CLIENT_SECRET;
16+
const refreshToken = (creds?.refreshToken) || process.env.STRAVA_REFRESH_TOKEN;
17+
18+
const source = hasUICreds ? 'UI' : 'ENV';
19+
console.log(`[Strava] Attempting token refresh. Source: ${source}, ClientID: ${clientId?.substring(0, 5)}..., Token: ${refreshToken?.substring(0, 8)}...`);
1420

1521
if (!clientId || !clientSecret || !refreshToken) {
16-
throw new Error('Missing Strava credentials. Please configure them in Settings.');
22+
throw new Error(`Missing Strava credentials (${source}). Please configure them in Settings or your environment variables.`);
1723
}
1824

1925
const params = new URLSearchParams();
@@ -44,9 +50,18 @@ export async function getStravaAccessToken(creds?: { clientId?: string; clientSe
4450
}
4551

4652
const data = await response.json();
53+
const scopes = data.scope || 'NOT_RETURNED';
54+
console.log(`[Strava] Refresh successful. Scopes received: ${scopes}. Response keys: ${Object.keys(data).join(', ')}`);
55+
4756
if (!data.access_token) {
4857
throw new Error('Strava token refresh response did not contain an access_token');
4958
}
59+
60+
// Check for activity read permission in the scopes
61+
if (scopes !== 'NOT_RETURNED' && !scopes.includes('activity:read')) {
62+
console.warn(`[Strava] WARNING: Token refreshed but missing 'activity:read' scope. Current permitted scopes: ${scopes}`);
63+
}
64+
5065
return data.access_token;
5166
}
5267

0 commit comments

Comments
 (0)