adding spotify authentication
This commit is contained in:
@@ -1,48 +1,58 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"net/http"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (store *Store) ListeningTo(c *gin.Context) {
|
func (store *Store) CompleteAuth(c *gin.Context) {
|
||||||
ctx := context.Background()
|
state := c.Query("state")
|
||||||
|
// code := c.Query("code")
|
||||||
|
|
||||||
log.Default().Println("Tets")
|
token, err := store.SpotifyAuth.Token(c.Request.Context(), state, c.Request)
|
||||||
|
|
||||||
playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, gin.H{"error": err.Error()})
|
c.String(http.StatusInternalServerError, "Couldn't get token: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Spotify says "nothing is currently playing"
|
store.SpotifyToken = token
|
||||||
if playing == nil || !playing.Playing || playing.Item == nil {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"playing": false,
|
|
||||||
"message": "User is not currently listening to anything",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract fields safely
|
// func (store *Store) ListeningTo(c *gin.Context) {
|
||||||
item := playing.Item
|
// ctx := context.Background()
|
||||||
artistName := ""
|
|
||||||
if len(item.Artists) > 0 {
|
|
||||||
artistName = item.Artists[0].Name
|
|
||||||
}
|
|
||||||
|
|
||||||
imgURL := ""
|
// playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(ctx)
|
||||||
if len(item.Album.Images) > 0 {
|
// if err != nil {
|
||||||
imgURL = item.Album.Images[0].URL
|
// c.JSON(500, gin.H{"error": err.Error()})
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
c.JSON(200, gin.H{
|
// // If Spotify says "nothing is currently playing"
|
||||||
"playing": true,
|
// if playing == nil || !playing.Playing || playing.Item == nil {
|
||||||
"song_name": item.Name,
|
// c.JSON(200, gin.H{
|
||||||
"artist_name": artistName,
|
// "playing": false,
|
||||||
"album_image": imgURL,
|
// "message": "User is not currently listening to anything",
|
||||||
})
|
// })
|
||||||
}
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Extract fields safely
|
||||||
|
// item := playing.Item
|
||||||
|
// artistName := ""
|
||||||
|
// if len(item.Artists) > 0 {
|
||||||
|
// artistName = item.Artists[0].Name
|
||||||
|
// }
|
||||||
|
|
||||||
|
// imgURL := ""
|
||||||
|
// if len(item.Album.Images) > 0 {
|
||||||
|
// imgURL = item.Album.Images[0].URL
|
||||||
|
// }
|
||||||
|
|
||||||
|
// c.JSON(200, gin.H{
|
||||||
|
// "playing": true,
|
||||||
|
// "song_name": item.Name,
|
||||||
|
// "artist_name": artistName,
|
||||||
|
// "album_image": imgURL,
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/zmb3/spotify/v2"
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
DB *gorm.DB
|
DB *gorm.DB
|
||||||
SpotifyClient *spotify.Client
|
SpotifyAuth *spotifyauth.Authenticator
|
||||||
|
SpotifyToken *oauth2.Token
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user