Skip to content

Commit

Permalink
Merge branch 'main' into #14
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jul 21, 2023
2 parents baae176 + f60e346 commit a448587
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
10 changes: 1 addition & 9 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"github.com/google/uuid"
"github.com/pkg/browser"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)

const (
port = "8888"
)

var (
auth *spotifyauth.Authenticator
state string
server *http.Server
)
Expand All @@ -34,7 +32,7 @@ func completeAuth(w http.ResponseWriter, r *http.Request) {
}

// set token config
if err := setTokenInfo(token); err != nil {
if err := saveToken(token); err != nil {
log.Fatal(err)
}

Expand All @@ -50,12 +48,6 @@ func completeAuth(w http.ResponseWriter, r *http.Request) {
}

func authorize() {
auth = spotifyauth.New(
spotifyauth.WithClientID(clientViper.GetString(SpotifyIDKeyName)),
spotifyauth.WithClientSecret(clientViper.GetString(SpotifySecretKeyName)),
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate),
)
server = &http.Server{Addr: fmt.Sprintf(":%s", port)}
state = uuid.New().String()
http.HandleFunc("/callback", completeAuth)
Expand Down
2 changes: 1 addition & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func initEnv() error {
return nil
}

func setTokenInfo(token *oauth2.Token) error {
func saveToken(token *oauth2.Token) error {
tokenViper.Set(AccessTokenKeyName, token.AccessToken)
tokenViper.Set(RefreshTokenKeyName, token.RefreshToken)
tokenViper.Set(ExpirationKeyName, token.Expiry.Unix())
Expand Down
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
)

var (
auth *spotifyauth.Authenticator
clientChannel = make(chan *spotify.Client, 1)
)

Expand All @@ -28,6 +29,13 @@ func main() {
log.Fatal(err)
}

auth = spotifyauth.New(
spotifyauth.WithClientID(clientViper.GetString(SpotifyIDKeyName)),
spotifyauth.WithClientSecret(clientViper.GetString(SpotifySecretKeyName)),
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate),
)

// check if token viper is set
if !isClientInfoSet() {
if err := askClientInfo(); err != nil {
Expand All @@ -38,8 +46,22 @@ func main() {
} else {
ctx := context.Background()
token := getToken()
httpClient := spotifyauth.New().Client(ctx, token)
clientChannel <- spotify.New(httpClient)

newToken, err := auth.RefreshToken(ctx, token)
if err != nil {
log.Fatalf("fail to get a new access token: %v", err)
}

// update an access token if it has expired
if token.AccessToken != newToken.AccessToken {
if err := saveToken(newToken); err != nil {
log.Fatalf("fail to save token: %v", err)
}
}

spotifyClient := spotify.New(auth.Client(ctx, newToken))

clientChannel <- spotifyClient
}

client := <-clientChannel
Expand Down

0 comments on commit a448587

Please sign in to comment.