mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 03:46:57 -08:00
fix spotify connection
This commit is contained in:
@@ -180,14 +180,33 @@ func ClearNowPlaying(userId int) {
|
||||
}
|
||||
|
||||
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(),
|
||||
`SELECT spotify_client_id, spotify_client_secret, spotify_access_token,
|
||||
spotify_refresh_token, spotify_token_expires
|
||||
FROM users WHERE pk = $1`,
|
||||
userId).Scan(&clientId, &clientSecret, &accessToken, &refreshToken, &expiresAt)
|
||||
userId).Scan(&clientIdPg, &clientSecretPg, &accessTokenPg, &refreshTokenPg, &expiresAtPg)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -77,9 +77,9 @@ type SpotifyCursors struct {
|
||||
func (h *SpotifyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
if path == "/authorize" {
|
||||
if path == "/scrobble/spotify/authorize" {
|
||||
h.handleAuthorize(w, r)
|
||||
} else if path == "/callback" {
|
||||
} else if path == "/scrobble/spotify/callback" {
|
||||
h.handleCallback(w, r)
|
||||
} else {
|
||||
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))
|
||||
fmt.Fprintf(os.Stderr, "handleAuthorize: userId=%s, clientId='%s', err=%v\n", userId, clientId, err)
|
||||
if err != nil || clientId == "" {
|
||||
http.Error(w, "Spotify credentials not configured", http.StatusBadRequest)
|
||||
return
|
||||
@@ -389,7 +390,11 @@ func getBaseURL(r *http.Request) string {
|
||||
if r.TLS != nil {
|
||||
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) {
|
||||
|
||||
@@ -434,3 +434,23 @@
|
||||
color: #8F8;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -98,6 +98,11 @@
|
||||
<button type="submit">Save Spotify Credentials</button>
|
||||
</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}}
|
||||
<p class="success">Spotify is connected and importing!</p>
|
||||
{{end}}
|
||||
|
||||
@@ -134,3 +134,34 @@ func updateSpotifyCredentialsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ func Start() {
|
||||
r.Get("/callback", http.HandlerFunc(scrobble.NewSpotifyHandler().ServeHTTP))
|
||||
})
|
||||
|
||||
r.Get("/settings/spotify-connect", spotifyConnectHandler)
|
||||
r.Get("/settings", settingsPageHandler())
|
||||
r.Post("/settings/generate-apikey", generateAPIKeyHandler)
|
||||
r.Post("/settings/update-spotify", updateSpotifyCredentialsHandler)
|
||||
|
||||
Reference in New Issue
Block a user