Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yanosea committed Feb 25, 2025
1 parent bb76842 commit fdb6acf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
14 changes: 10 additions & 4 deletions app/application/spotlike/auth_usecase.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package spotlike

import ()
import (
spotifyauth "github.com/zmb3/spotify/v2/auth"
)

// authUseCase is a struct that contains the use case of authenticating the Spotify client.
type authUseCase struct{}
type authUseCase struct {
Authenticator *spotifyauth.Authenticator
}

// NewAuthUseCase returns a new instance of the AuthUseCase struct.
func NewAuthUseCase() *authUseCase {
return &authUseCase{}
func NewAuthUseCase(authenticator *spotifyauth.Authenticator) *authUseCase {
return &authUseCase{
Authenticator: authenticator,
}
}

// Run returns the output of the AuthUseCase.
Expand Down
27 changes: 14 additions & 13 deletions app/application/spotlike/get_auth_url_usecase.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package spotlike

import ()
import (
"github.com/thanhpk/randstr"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)

// getAuthUrlUseCase is a struct that contains the use case of getting the authentication URL.
type getAuthUrlUseCase struct{}

// NewGetAuthUrlUseCase returns a new instance of the GetAuthUrlUseCase struct.
func NewGetAuthUrlUseCase() *getAuthUrlUseCase {
return &getAuthUrlUseCase{}
type getAuthUrlUseCase struct {
Authenticator *spotifyauth.Authenticator
}

type GetAuthUrlUseCaseOutputDto struct {
AuthUrl string
// NewGetAuthUrlUseCase returns a new instance of the GetAuthUrlUseCase struct.
func NewGetAuthUrlUseCase(authenticator *spotifyauth.Authenticator) *getAuthUrlUseCase {
return &getAuthUrlUseCase{
Authenticator: authenticator,
}
}

// Run returns the output of the GetAuthUrlUseCase.
func (uc *getAuthUrlUseCase) Run() *GetAuthUrlUseCaseOutputDto {
return &GetAuthUrlUseCaseOutputDto{
AuthUrl: "https://accounts.spotify.com/authorize?client_id",
}
// Run returns the authentication URL.
func (uc *getAuthUrlUseCase) Run() string {
return uc.Authenticator.AuthURL(randstr.Hex(11))
}
34 changes: 34 additions & 0 deletions app/application/spotlike/get_authenticator_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spotlike

import (
spotifyauth "github.com/zmb3/spotify/v2/auth"

"github.com/yanosea/spotlike/app/config"
)

// getAuthenticatorUseCase is a struct that contains the use case of getting the authenticator.
type getAuthenticatorUseCase struct {
SpotlikeConfig *config.SpotlikeConfig
}

// NewGetAuthenticatorUseCase returns a new instance of the GetAuthenticatorUseCase struct.
func NewGetAuthenticatorUseCase(conf *config.SpotlikeConfig) *getAuthenticatorUseCase {
return &getAuthenticatorUseCase{
SpotlikeConfig: conf,
}
}

// Run returns the authenticator.
func (uc *getAuthenticatorUseCase) Run() *spotifyauth.Authenticator {
return spotifyauth.New(
spotifyauth.WithScopes(
spotifyauth.ScopeUserFollowRead,
spotifyauth.ScopeUserFollowModify,
spotifyauth.ScopeUserLibraryRead,
spotifyauth.ScopeUserLibraryModify,
),
spotifyauth.WithClientID(uc.SpotlikeConfig.SpotifyID),
spotifyauth.WithClientSecret(uc.SpotlikeConfig.SpotifySecret),
spotifyauth.WithRedirectURL(uc.SpotlikeConfig.SpotifyRedirectUri),
)
}
25 changes: 18 additions & 7 deletions app/presentation/cli/spotlike/command/spotlike/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

spotlikeApp "github.com/yanosea/spotlike/app/application/spotlike"
baseConfig "github.com/yanosea/spotlike/app/config"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/config"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/formatter"
"github.com/yanosea/spotlike/app/presentation/cli/spotlike/presenter"
Expand Down Expand Up @@ -143,20 +144,30 @@ func runAuth(conf *config.SpotlikeCliConfig, output *string) error {
}
}

bc := &baseConfig.SpotlikeConfig{
SpotifyID: conf.SpotifyID,
SpotifySecret: conf.SpotifySecret,
SpotifyRedirectUri: conf.SpotifyRedirectUri,
SpotifyRefreshToken: conf.SpotifyRefreshToken,
}

gauc := spotlikeApp.NewGetAuthenticatorUseCase(bc)
authenticator := gauc.Run()

if conf.SpotifyRefreshToken == "" {
guc := spotlikeApp.NewGetAuthUrlUseCase(conf)
presenter.Print(os.Stdout, guc.Run())
guuc := spotlikeApp.NewGetAuthUrlUseCase(authenticator)
presenter.Print(os.Stdout, guuc.Run())

auc := spotlikeApp.NewAuthUseCase(conf)
if err := auc.Run(conf); err != nil {
auc := spotlikeApp.NewAuthUseCase(authenticator)
if err := auc.Run(); err != nil {
o := formatter.Red("❌ Failed to authenticate... Please try again...")
*output = o
return err
}
} else {
uc := spotlikeApp.NewRefreshUseCase(conf)
if err := uc.Refresh(); err != nil {
if err := uc.Run(conf); err != nil {
ruc := spotlikeApp.NewRefreshUseCase(conf)
if err := ruc.Refresh(); err != nil {
if err := ruc.Run(); err != nil {
o := formatter.Red("❌ Failed to refresh... Please try again...")
*output = o
return err
Expand Down

0 comments on commit fdb6acf

Please sign in to comment.