From 7ed0eacbbdb8f46a7765541d5b7f1bc88eb051b8 Mon Sep 17 00:00:00 2001 From: Yi-Jyun Pan Date: Sat, 23 Mar 2024 22:02:54 +0800 Subject: [PATCH] fix(auth/login): zeabur auth login failed if the token has expired --- internal/cmd/auth/login/login.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/internal/cmd/auth/login/login.go b/internal/cmd/auth/login/login.go index 12330d6..36e70b0 100644 --- a/internal/cmd/auth/login/login.go +++ b/internal/cmd/auth/login/login.go @@ -3,8 +3,11 @@ package login import ( "context" + "errors" "fmt" + "strings" + "github.com/hasura/go-graphql-client" "github.com/spf13/cobra" "github.com/spf13/viper" "golang.org/x/oauth2" @@ -58,13 +61,21 @@ func RunLogin(f *cmdutil.Factory, opts *Options) error { f.ApiClient = opts.NewClient(f.Config.GetTokenString()) user, err := f.ApiClient.GetUserInfo(context.Background()) if err != nil { - return fmt.Errorf("failed to get user info: %w", err) + var graphqlErrors graphql.Errors + if errors.As(err, &graphqlErrors) && + len(graphqlErrors) > 0 && + strings.HasPrefix(graphqlErrors[0].Message, "401 Unauthorized") { + f.Log.Debug("Token is expired or invalid, need to login again") + } else { + return fmt.Errorf("failed to get user info: %w", err) + } + } else { + f.Log.Debugw("Already logged in", "token string", f.Config.GetTokenString(), + "token detail", f.Config.GetToken(), "user", user) + f.Log.Infof("Already logged in as %s, "+ + "if you want to use a different account, please logout first", user.Name) + return nil } - f.Log.Debugw("Already logged in", "token string", f.Config.GetTokenString(), - "token detail", f.Config.GetToken(), "user", user) - f.Log.Infof("Already logged in as %s, "+ - "if you want to use a different account, please logout first", user.Name) - return nil } var (