Spotify unauthenticated: throttled log + admin reconnect on Listening widget
Deploy with Docker Compose / deploy (push) Successful in 40s

- Backend logs a rate-limited (1/h) "not authenticated" line when a resolver
  finds the Spotify client nil instead of silently degrading to null.
- New public-but-gated spotifyNeedsReauth query: false for everyone except
  an admin session whose backend has no client.
- The /stp Listening widget offers admins a "Reconnect Spotify" button that
  hits /api/spotify/auth and navigates to the returned authorisation URL;
  non-admins still see the widget degrade as before.

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-09-22 22:54:48 +01:00
co-authored by Claude Code
parent 241acb82d9
commit ff186f04a7
6 changed files with 163 additions and 19 deletions
+21
View File
@@ -3,6 +3,7 @@ package handlers
import (
"crypto/rand"
"encoding/hex"
"log"
"sync"
"time"
@@ -66,6 +67,12 @@ type Store struct {
// restarting an OAuth flow that takes seconds.
spotifyStatesMu sync.Mutex
spotifyStates map[string]time.Time
// spotifyNilLoggedAt rate-limits the "Spotify not authenticated" log line:
// every page visit hits spotifyRecent, so an unauthenticated backend would
// otherwise write a flood of identical lines. nil means "recently logged".
spotifyNilLoggedMu sync.Mutex
spotifyNilLoggedAt time.Time
}
// spotifyStateTTL is how long an unused Spotify OAuth nonce stays valid. Long
@@ -123,6 +130,20 @@ func (s *Store) ConsumeSpotifyState(state string) bool {
// only when the entry is present and inside its TTL; each "Set..." method
// stores a value and stamps it.
// LogSpotifyUnauthenticated logs that the Spotify OAuth flow has never been
// completed (or a stored token stopped working), with a where hint. It is
// rate-limited to one line per hour — see spotifyNilLoggedAt — because
// spotifyRecent is hit by every home-page visit.
func (s *Store) LogSpotifyUnauthenticated(where string) {
s.spotifyNilLoggedMu.Lock()
defer s.spotifyNilLoggedMu.Unlock()
if time.Since(s.spotifyNilLoggedAt) < time.Hour {
return
}
s.spotifyNilLoggedAt = time.Now()
log.Printf("[Spotify] not authenticated (seen in %s) — an admin can reconnect via GET /api/spotify/auth", where)
}
// CachedRecentSongs returns the cached Spotify recently-played list if it is
// within its one-minute TTL. An empty list counts as stale, so an empty
// listening history is re-fetched every time (unchanged behaviour).