Skip to content

Commit fa98ba2

Browse files
Implement Refresh token to allow multiple api calls per login
1 parent 5c5667d commit fa98ba2

File tree

2 files changed

+209
-144
lines changed

2 files changed

+209
-144
lines changed

src/server.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ If the user asks "what are my top tracks", "show my top songs", "my top Spotify
9090
If the user asks "what did I recently play", "my recent tracks", "recently played", or similar questions about their recent listening history, use the loginToSpotify tool first to provide the login link.
9191
If the user asks about Spotify login, authentication, or wants to access their Spotify data, use the loginToSpotify tool first.
9292
If the user asks "am I logged in?", "am I login?", "check my login status", or similar questions about their Spotify login status, use the checkSpotifyLogin tool.
93-
If the user provides an authorization code from Spotify callback and wants to see their top artists, use the getUserTopArtists tool with the authorization code.
94-
If the user provides an authorization code from Spotify callback and wants to see their top tracks, use the getUserTopTracks tool with the authorization code.
95-
If the user provides an authorization code from Spotify callback and wants to see their recently played tracks, use the getUserRecentlyPlayed tool with the authorization code.
96-
If the user provides an authorization code from Spotify callback and wants to see their profile data, use the getUserSpotifyProfile tool with the authorization code.
93+
If the user asks for their top artists, use the getUserTopArtists tool (authCode is optional if already logged in).
94+
If the user asks for their top tracks, use the getUserTopTracks tool (authCode is optional if already logged in).
95+
If the user asks for their recently played tracks, use the getUserRecentlyPlayed tool (authCode is optional if already logged in).
96+
If the user asks for their profile data, use the getUserSpotifyProfile tool (authCode is optional if already logged in).
9797
`,
9898

9999
messages: convertToModelMessages(processedMessages),
@@ -167,6 +167,30 @@ export default {
167167
}
168168
}
169169

170+
if (url.pathname === "/get-tokens") {
171+
const storedTokens = await env.AUTH_CODES.get("spotifyTokens");
172+
if (storedTokens) {
173+
return new Response(storedTokens);
174+
} else {
175+
return new Response(JSON.stringify({ error: "No tokens found" }), {
176+
status: 404
177+
});
178+
}
179+
}
180+
181+
if (url.pathname === "/store-tokens") {
182+
if (request.method === "POST") {
183+
const tokenData = await request.json();
184+
await env.AUTH_CODES.put("spotifyTokens", JSON.stringify(tokenData), {
185+
expirationTtl: 3600
186+
}); // 1 hour TTL
187+
return new Response(JSON.stringify({ success: true }));
188+
}
189+
return new Response(JSON.stringify({ error: "Method not allowed" }), {
190+
status: 405
191+
});
192+
}
193+
170194
if (url.pathname === "/test") {
171195
return new Response(
172196
"Test endpoint is working! Visit /callback?code=test123 to test the callback."

0 commit comments

Comments
 (0)