Documentation
¶
Overview ¶
Package auth handles Spotify OAuth2 with PKCE: interactive login via a local callback server, token persistence on disk, and proactive token refresh to avoid blocking API calls on an expiring token.
Typical flow:
token, _ := auth.LoadToken()
if token == nil {
token, _ = auth.Login(ctx, authenticator, redirectURL)
_ = auth.SaveToken(token)
}
httpClient, saveErrCh, cleanup, _ := auth.NewSavingClient(ctx, authenticator, token)
defer cleanup()
NewSavingClient returns an *http.Client that refreshes and re-persists the token automatically; its cleanup stops the proactive-refresh goroutine. Refresh failures that can't block the caller (disk write errors) are surfaced on saveErrCh so the UI can warn the user.
Index ¶
- func LoadToken() (*oauth2.Token, error)
- func Login(ctx context.Context, a *spotifyauth.Authenticator, redirectURL string) (*oauth2.Token, error)
- func NewAuthenticator(clientID, redirectURL string) *spotifyauth.Authenticator
- func NewSavingClient(ctx context.Context, a *spotifyauth.Authenticator, token *oauth2.Token) (*http.Client, <-chan error, func(), error)
- func SaveToken(token *oauth2.Token) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Login ¶
func Login(ctx context.Context, a *spotifyauth.Authenticator, redirectURL string) (*oauth2.Token, error)
Login runs the interactive PKCE flow: spins up a local callback server, opens the browser, and blocks until the user completes auth. Cancel ctx to abort a login that is stuck waiting for the browser callback.
func NewAuthenticator ¶
func NewAuthenticator(clientID, redirectURL string) *spotifyauth.Authenticator
func NewSavingClient ¶
func NewSavingClient(ctx context.Context, a *spotifyauth.Authenticator, token *oauth2.Token) (*http.Client, <-chan error, func(), error)
NewSavingClient creates an HTTP client that auto-refreshes OAuth tokens and persists them to disk on each refresh. The returned cleanup function stops the proactive-refresh goroutine; callers must invoke it on shutdown. saveErrCh emits persistence failures so the caller can surface them to the user (e.g. a status banner); it is buffered and lossy on full. ctx is the parent lifetime: when it is cancelled, the proactive-refresh goroutine exits and in-flight oauth2 refresh requests are cancelled too.
Types ¶
This section is empty.