Skip to content

Commit

Permalink
update auth (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 6, 2023
1 parent 2320a1e commit 9b064b4
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
spotifyauth "github.com/JunNishimura/spotify/v2/auth"
"github.com/google/uuid"
"github.com/pkg/browser"
"golang.org/x/oauth2"
)

const (
Expand Down Expand Up @@ -68,8 +69,8 @@ func (a *Client) completeAuth(w http.ResponseWriter, r *http.Request) {
log.Fatalf("state mismatch: got = %s, expected = %s\n", getState, a.state)
}

if err := a.cfg.SetToken(token); err != nil {
log.Fatalf("fail to set token: %v", err)
if err := a.setToken(token); err != nil {
log.Fatal(err)
}

a.SpotifyChannel <- spotify.New(a.auth.Client(r.Context(), token))
Expand All @@ -80,3 +81,17 @@ func (a *Client) completeAuth(w http.ResponseWriter, r *http.Request) {
}
}()
}

func (a *Client) setToken(token *oauth2.Token) error {
if err := a.cfg.Set(config.AccessTokenKey, token.AccessToken); err != nil {
return fmt.Errorf("fail to set access token: %v", err)
}
if err := a.cfg.Set(config.RefreshTokenKey, token.RefreshToken); err != nil {
return fmt.Errorf("fail to set refresh token: %v", err)
}
if err := a.cfg.Set(config.ExpirationKey, token.Expiry.Unix()); err != nil {
return fmt.Errorf("fail to set expiration: %v", err)
}

return nil
}

0 comments on commit 9b064b4

Please sign in to comment.