From da91d012b12d7f73e584f5ab292d2c0daa8adcb9 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 30 Dec 2025 01:49:05 +0530 Subject: [PATCH] Fix whoami path response --- cli/command/auth/login.go | 27 +++++++++------------------ cli/command/publish/publish.go | 2 +- cli/command/whoami/whoami.go | 20 +++++++++----------- pkg/config/configfile/file.go | 2 -- 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/cli/command/auth/login.go b/cli/command/auth/login.go index e34f141..e3d141c 100644 --- a/cli/command/auth/login.go +++ b/cli/command/auth/login.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strconv" "wpm/cli" "wpm/cli/command" "wpm/pkg/api" @@ -72,13 +71,7 @@ func tokenStdinPrompt(ctx context.Context, wpmCli command.Cli, opts *loginOption return nil } -type AuthResponse struct { - Uid int `json:"uid"` - Tid int `json:"tid"` - Username string `json:"username"` -} - -func validateToken(wpmCli command.Cli, token string) (*AuthResponse, error) { +func validateToken(wpmCli command.Cli, token string) (string, error) { client, err := api.NewRESTClient(api.ClientOptions{ Log: wpmCli.Err(), AuthToken: token, @@ -87,16 +80,16 @@ func validateToken(wpmCli command.Cli, token string) (*AuthResponse, error) { LogColorize: !wpmTerm.IsColorDisabled() && term.IsTerminal(wpmCli.Err().FD()), }) if err != nil { - return nil, err + return "", err } - var response AuthResponse + var response string err = wpmCli.Progress().RunWithProgress("validating token", func() error { return client.Get("/-/whoami", &response) }, wpmCli.Err()) if err != nil { - return nil, err + return "", err } - return &response, nil + return response, nil } func runLogin(ctx context.Context, wpmCli command.Cli, opts loginOptions) error { @@ -110,25 +103,23 @@ func runLogin(ctx context.Context, wpmCli command.Cli, opts loginOptions) error } } - resp, err := validateToken(wpmCli, opts.token) + username, err := validateToken(wpmCli, opts.token) if err != nil { return err } - if resp == nil || resp.Username == "" || resp.Tid == 0 || resp.Uid == 0 { + if username == "" { return errors.New(aec.RedF.Apply("unable to resolve identity from token")) } cfg := wpmCli.ConfigFile() cfg.AuthToken = opts.token - cfg.DefaultUser = resp.Username - cfg.DefaultUId = strconv.Itoa(resp.Uid) - cfg.DefaultTId = strconv.Itoa(resp.Tid) + cfg.DefaultUser = username if err := cfg.Save(); err != nil { return err } - _, _ = fmt.Fprintf(wpmCli.Out(), "welcome %s!\n", resp.Username) + _, _ = fmt.Fprintf(wpmCli.Out(), "welcome %s!\n", username) return nil } diff --git a/cli/command/publish/publish.go b/cli/command/publish/publish.go index 1f96391..d4648be 100644 --- a/cli/command/publish/publish.go +++ b/cli/command/publish/publish.go @@ -199,7 +199,7 @@ func runPublish(ctx context.Context, wpmCli command.Cli, opts publishOptions) er } cfg := wpmCli.ConfigFile() - if cfg.AuthToken == "" || cfg.DefaultTId == "" { + if cfg.DefaultUser == "" || cfg.AuthToken == "" { return errors.New("user must be logged in to perform this action") } diff --git a/cli/command/whoami/whoami.go b/cli/command/whoami/whoami.go index 3a45b72..647af3d 100644 --- a/cli/command/whoami/whoami.go +++ b/cli/command/whoami/whoami.go @@ -4,7 +4,6 @@ import ( "fmt" "wpm/cli" "wpm/cli/command" - "wpm/cli/command/auth" "wpm/pkg/api" "wpm/pkg/config" wpmTerm "wpm/pkg/term" @@ -26,7 +25,7 @@ func NewWhoamiCommand(wpmCli command.Cli) *cobra.Command { return cmd } -func validateToken(wpmCli command.Cli, token string) (*auth.AuthResponse, error) { +func validateToken(wpmCli command.Cli, token string) (string, error) { client, err := api.NewRESTClient(api.ClientOptions{ Log: wpmCli.Err(), AuthToken: token, @@ -38,16 +37,16 @@ func validateToken(wpmCli command.Cli, token string) (*auth.AuthResponse, error) LogColorize: !wpmTerm.IsColorDisabled() && term.IsTerminal(wpmCli.Err().FD()), }) if err != nil { - return nil, err + return "", err } - var response auth.AuthResponse + var response string err = client.Get("/-/whoami", &response) if err != nil { - return nil, err + return "", err } - return &response, nil + return response, nil } func runWhoami(wpmCli command.Cli) error { @@ -56,21 +55,20 @@ func runWhoami(wpmCli command.Cli) error { return errors.New("user must be logged in to perform this action") } - var resp *auth.AuthResponse + var username string err := wpmCli.Progress().RunWithProgress("", func() error { var err error - resp, err = validateToken(wpmCli, cfg.AuthToken) + username, err = validateToken(wpmCli, cfg.AuthToken) return err }, wpmCli.Out()) if err != nil { return err } - if resp.Username == "" { + if username == "" { return errors.New(aec.RedF.Apply("unable to resolve identity from token")) } - _, _ = fmt.Fprintln(wpmCli.Out(), resp.Username) - + _, _ = fmt.Fprintln(wpmCli.Out(), username) return nil } diff --git a/pkg/config/configfile/file.go b/pkg/config/configfile/file.go index 8082477..a55d7cb 100644 --- a/pkg/config/configfile/file.go +++ b/pkg/config/configfile/file.go @@ -24,8 +24,6 @@ type ConfigFile struct { Filename string `json:"-"` // Note: for internal use only AuthToken string `json:"authToken,omitempty"` DefaultUser string `json:"defaultUser,omitempty"` - DefaultUId string `json:"defaultUid,omitempty"` - DefaultTId string `json:"defaultTid,omitempty"` UsersAuthTokens map[string]UsersAuthConfig `json:"usersAuthTokens,omitempty"` PluginsAuthToken map[string]PluginsAuthConfig `json:"pluginsAuthToken,omitempty"` }