Skip to content

Commit 659b68f

Browse files
committed
fix spotify connection
1 parent d2d325b commit 659b68f

6 files changed

Lines changed: 85 additions & 4 deletions

File tree

scrobble/scrobble.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,33 @@ func ClearNowPlaying(userId int) {
180180
}
181181

182182
func GetUserSpotifyCredentials(userId int) (clientId, clientSecret, accessToken, refreshToken string, expiresAt time.Time, err error) {
183+
var clientIdPg, clientSecretPg, accessTokenPg, refreshTokenPg pgtype.Text
184+
var expiresAtPg pgtype.Timestamptz
183185
err = db.Pool.QueryRow(context.Background(),
184186
`SELECT spotify_client_id, spotify_client_secret, spotify_access_token,
185187
spotify_refresh_token, spotify_token_expires
186188
FROM users WHERE pk = $1`,
187-
userId).Scan(&clientId, &clientSecret, &accessToken, &refreshToken, &expiresAt)
189+
userId).Scan(&clientIdPg, &clientSecretPg, &accessTokenPg, &refreshTokenPg, &expiresAtPg)
188190
if err != nil {
189191
return "", "", "", "", time.Time{}, err
190192
}
193+
194+
if clientIdPg.Status == pgtype.Present {
195+
clientId = clientIdPg.String
196+
}
197+
if clientSecretPg.Status == pgtype.Present {
198+
clientSecret = clientSecretPg.String
199+
}
200+
if accessTokenPg.Status == pgtype.Present {
201+
accessToken = accessTokenPg.String
202+
}
203+
if refreshTokenPg.Status == pgtype.Present {
204+
refreshToken = refreshTokenPg.String
205+
}
206+
if expiresAtPg.Status == pgtype.Present {
207+
expiresAt = expiresAtPg.Time
208+
}
209+
191210
return clientId, clientSecret, accessToken, refreshToken, expiresAt, nil
192211
}
193212

scrobble/spotify.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ type SpotifyCursors struct {
7777
func (h *SpotifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7878
path := r.URL.Path
7979

80-
if path == "/authorize" {
80+
if path == "/scrobble/spotify/authorize" {
8181
h.handleAuthorize(w, r)
82-
} else if path == "/callback" {
82+
} else if path == "/scrobble/spotify/callback" {
8383
h.handleCallback(w, r)
8484
} else {
8585
http.Error(w, "Not found", http.StatusNotFound)
@@ -94,6 +94,7 @@ func (h *SpotifyHandler) handleAuthorize(w http.ResponseWriter, r *http.Request)
9494
}
9595

9696
clientId, _, _, _, _, err := GetUserSpotifyCredentials(userIdToInt(userId))
97+
fmt.Fprintf(os.Stderr, "handleAuthorize: userId=%s, clientId='%s', err=%v\n", userId, clientId, err)
9798
if err != nil || clientId == "" {
9899
http.Error(w, "Spotify credentials not configured", http.StatusBadRequest)
99100
return
@@ -389,7 +390,11 @@ func getBaseURL(r *http.Request) string {
389390
if r.TLS != nil {
390391
scheme = "https"
391392
}
392-
return scheme + "://" + r.Host
393+
host := r.Host
394+
if host == "localhost:1234" || host == "localhost" {
395+
host = "127.0.0.1:1234"
396+
}
397+
return scheme + "://" + host
393398
}
394399

395400
func GetSpotifyAuthURL(userId int, baseURL string) (string, error) {

static/style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,23 @@
434434
color: #8F8;
435435
margin-top: 10px;
436436
}
437+
438+
.info {
439+
color: #888;
440+
font-size: 14px;
441+
margin-top: 10px;
442+
}
443+
444+
a.button {
445+
display: inline-block;
446+
padding: 10px 20px;
447+
background: #1DB954;
448+
color: #fff;
449+
text-decoration: none;
450+
border-radius: 25px;
451+
font-weight: bold;
452+
}
453+
454+
a.button:hover {
455+
background: #1ed760;
456+
}

templates/settings.gohtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
<button type="submit">Save Spotify Credentials</button>
9999
</form>
100100

101+
{{if and .SpotifyClientId (not .SpotifyConnected)}}
102+
<p><a href="/settings/spotify-connect" class="button">Connect Spotify</a></p>
103+
<p class="info">Click to authorize Muzi to access your Spotify account.</p>
104+
{{end}}
105+
101106
{{if .SpotifyConnected}}
102107
<p class="success">Spotify is connected and importing!</p>
103108
{{end}}

web/settings.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,34 @@ func updateSpotifyCredentialsHandler(w http.ResponseWriter, r *http.Request) {
134134

135135
http.Redirect(w, r, "/settings", http.StatusSeeOther)
136136
}
137+
138+
func spotifyConnectHandler(w http.ResponseWriter, r *http.Request) {
139+
username := getLoggedInUsername(r)
140+
if username == "" {
141+
http.Redirect(w, r, "/login", http.StatusSeeOther)
142+
return
143+
}
144+
145+
userId, err := getUserIdByUsername(r.Context(), username)
146+
if err != nil {
147+
http.Error(w, "User not found", http.StatusInternalServerError)
148+
return
149+
}
150+
151+
user, err := scrobble.GetUserById(userId)
152+
if err != nil {
153+
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: GetUserById error: %v\n", err)
154+
http.Redirect(w, r, "/settings", http.StatusSeeOther)
155+
return
156+
}
157+
158+
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: userId=%d, SpotifyClientId=%v\n", userId, user.SpotifyClientId)
159+
160+
if user.SpotifyClientId == nil || *user.SpotifyClientId == "" {
161+
fmt.Fprintf(os.Stderr, "spotifyConnectHandler: SpotifyClientId is nil or empty, redirecting to settings\n")
162+
http.Redirect(w, r, "/settings", http.StatusSeeOther)
163+
return
164+
}
165+
166+
http.Redirect(w, r, fmt.Sprintf("/scrobble/spotify/authorize?user_id=%d", userId), http.StatusSeeOther)
167+
}

web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func Start() {
9191
r.Get("/callback", http.HandlerFunc(scrobble.NewSpotifyHandler().ServeHTTP))
9292
})
9393

94+
r.Get("/settings/spotify-connect", spotifyConnectHandler)
9495
r.Get("/settings", settingsPageHandler())
9596
r.Post("/settings/generate-apikey", generateAPIKeyHandler)
9697
r.Post("/settings/update-spotify", updateSpotifyCredentialsHandler)

0 commit comments

Comments
 (0)