fix spotify connection

This commit is contained in:
2026-02-28 00:13:37 -08:00
parent d2d325ba46
commit 659b68f11d
6 changed files with 85 additions and 4 deletions

View File

@@ -180,14 +180,33 @@ func ClearNowPlaying(userId int) {
} }
func GetUserSpotifyCredentials(userId int) (clientId, clientSecret, accessToken, refreshToken string, expiresAt time.Time, err error) { func GetUserSpotifyCredentials(userId int) (clientId, clientSecret, accessToken, refreshToken string, expiresAt time.Time, err error) {
var clientIdPg, clientSecretPg, accessTokenPg, refreshTokenPg pgtype.Text
var expiresAtPg pgtype.Timestamptz
err = db.Pool.QueryRow(context.Background(), err = db.Pool.QueryRow(context.Background(),
`SELECT spotify_client_id, spotify_client_secret, spotify_access_token, `SELECT spotify_client_id, spotify_client_secret, spotify_access_token,
spotify_refresh_token, spotify_token_expires spotify_refresh_token, spotify_token_expires
FROM users WHERE pk = $1`, FROM users WHERE pk = $1`,
userId).Scan(&clientId, &clientSecret, &accessToken, &refreshToken, &expiresAt) userId).Scan(&clientIdPg, &clientSecretPg, &accessTokenPg, &refreshTokenPg, &expiresAtPg)
if err != nil { if err != nil {
return "", "", "", "", time.Time{}, err return "", "", "", "", time.Time{}, err
} }
if clientIdPg.Status == pgtype.Present {
clientId = clientIdPg.String
}
if clientSecretPg.Status == pgtype.Present {
clientSecret = clientSecretPg.String
}
if accessTokenPg.Status == pgtype.Present {
accessToken = accessTokenPg.String
}
if refreshTokenPg.Status == pgtype.Present {
refreshToken = refreshTokenPg.String
}
if expiresAtPg.Status == pgtype.Present {
expiresAt = expiresAtPg.Time
}
return clientId, clientSecret, accessToken, refreshToken, expiresAt, nil return clientId, clientSecret, accessToken, refreshToken, expiresAt, nil
} }

View File

@@ -77,9 +77,9 @@ type SpotifyCursors struct {
func (h *SpotifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SpotifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
if path == "/authorize" { if path == "/scrobble/spotify/authorize" {
h.handleAuthorize(w, r) h.handleAuthorize(w, r)
} else if path == "/callback" { } else if path == "/scrobble/spotify/callback" {
h.handleCallback(w, r) h.handleCallback(w, r)
} else { } else {
http.Error(w, "Not found", http.StatusNotFound) http.Error(w, "Not found", http.StatusNotFound)
@@ -94,6 +94,7 @@ func (h *SpotifyHandler) handleAuthorize(w http.ResponseWriter, r *http.Request)
} }
clientId, _, _, _, _, err := GetUserSpotifyCredentials(userIdToInt(userId)) clientId, _, _, _, _, err := GetUserSpotifyCredentials(userIdToInt(userId))
fmt.Fprintf(os.Stderr, "handleAuthorize: userId=%s, clientId='%s', err=%v\n", userId, clientId, err)
if err != nil || clientId == "" { if err != nil || clientId == "" {
http.Error(w, "Spotify credentials not configured", http.StatusBadRequest) http.Error(w, "Spotify credentials not configured", http.StatusBadRequest)
return return
@@ -389,7 +390,11 @@ func getBaseURL(r *http.Request) string {
if r.TLS != nil { if r.TLS != nil {
scheme = "https" scheme = "https"
} }
return scheme + "://" + r.Host host := r.Host
if host == "localhost:1234" || host == "localhost" {
host = "127.0.0.1:1234"
}
return scheme + "://" + host
} }
func GetSpotifyAuthURL(userId int, baseURL string) (string, error) { func GetSpotifyAuthURL(userId int, baseURL string) (string, error) {

View File

@@ -434,3 +434,23 @@
color: #8F8; color: #8F8;
margin-top: 10px; margin-top: 10px;
} }
.info {
color: #888;
font-size: 14px;
margin-top: 10px;
}
a.button {
display: inline-block;
padding: 10px 20px;
background: #1DB954;
color: #fff;
text-decoration: none;
border-radius: 25px;
font-weight: bold;
}
a.button:hover {
background: #1ed760;
}

View File

@@ -98,6 +98,11 @@
<button type="submit">Save Spotify Credentials</button> <button type="submit">Save Spotify Credentials</button>
</form> </form>
{{if and .SpotifyClientId (not .SpotifyConnected)}}
<p><a href="/settings/spotify-connect" class="button">Connect Spotify</a></p>
<p class="info">Click to authorize Muzi to access your Spotify account.</p>
{{end}}
{{if .SpotifyConnected}} {{if .SpotifyConnected}}
<p class="success">Spotify is connected and importing!</p> <p class="success">Spotify is connected and importing!</p>
{{end}} {{end}}

View File

@@ -134,3 +134,34 @@ func updateSpotifyCredentialsHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/settings", http.StatusSeeOther) http.Redirect(w, r, "/settings", http.StatusSeeOther)
} }
func spotifyConnectHandler(w http.ResponseWriter, r *http.Request) {
username := getLoggedInUsername(r)
if username == "" {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
userId, err := getUserIdByUsername(r.Context(), username)
if err != nil {
http.Error(w, "User not found", http.StatusInternalServerError)
return
}
user, err := scrobble.GetUserById(userId)
if err != nil {
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: GetUserById error: %v\n", err)
http.Redirect(w, r, "/settings", http.StatusSeeOther)
return
}
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: userId=%d, SpotifyClientId=%v\n", userId, user.SpotifyClientId)
if user.SpotifyClientId == nil || *user.SpotifyClientId == "" {
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: SpotifyClientId is nil or empty, redirecting to settings\n")
http.Redirect(w, r, "/settings", http.StatusSeeOther)
return
}
http.Redirect(w, r, fmt.Sprintf("/scrobble/spotify/authorize?user_id=%d", userId), http.StatusSeeOther)
}

View File

@@ -91,6 +91,7 @@ func Start() {
r.Get("/callback", http.HandlerFunc(scrobble.NewSpotifyHandler().ServeHTTP)) r.Get("/callback", http.HandlerFunc(scrobble.NewSpotifyHandler().ServeHTTP))
}) })
r.Get("/settings/spotify-connect", spotifyConnectHandler)
r.Get("/settings", settingsPageHandler()) r.Get("/settings", settingsPageHandler())
r.Post("/settings/generate-apikey", generateAPIKeyHandler) r.Post("/settings/generate-apikey", generateAPIKeyHandler)
r.Post("/settings/update-spotify", updateSpotifyCredentialsHandler) r.Post("/settings/update-spotify", updateSpotifyCredentialsHandler)