Skip to content

Commit 3ea07b0

Browse files
authored
fix(auth/login): zeabur auth login failed if the token has expired (#95)
<!-- Thank you for opening a PR! We really appreciate you taking the time to help out 🙌 --> #### Description (required) - fix(auth/login): zeabur auth login failed if the token has expired <!-- Please describe the change you are proposing, and why --> #### Related issues & labels (optional) - Closes ZEA-2723 - Suggested label: bug
2 parents 61f7a7f + 7ed0eac commit 3ea07b0

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

internal/cmd/auth/login/login.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package login
33

44
import (
55
"context"
6+
"errors"
67
"fmt"
8+
"strings"
79

10+
"github.com/hasura/go-graphql-client"
811
"github.com/spf13/cobra"
912
"github.com/spf13/viper"
1013
"golang.org/x/oauth2"
@@ -58,13 +61,21 @@ func RunLogin(f *cmdutil.Factory, opts *Options) error {
5861
f.ApiClient = opts.NewClient(f.Config.GetTokenString())
5962
user, err := f.ApiClient.GetUserInfo(context.Background())
6063
if err != nil {
61-
return fmt.Errorf("failed to get user info: %w", err)
64+
var graphqlErrors graphql.Errors
65+
if errors.As(err, &graphqlErrors) &&
66+
len(graphqlErrors) > 0 &&
67+
strings.HasPrefix(graphqlErrors[0].Message, "401 Unauthorized") {
68+
f.Log.Debug("Token is expired or invalid, need to login again")
69+
} else {
70+
return fmt.Errorf("failed to get user info: %w", err)
71+
}
72+
} else {
73+
f.Log.Debugw("Already logged in", "token string", f.Config.GetTokenString(),
74+
"token detail", f.Config.GetToken(), "user", user)
75+
f.Log.Infof("Already logged in as %s, "+
76+
"if you want to use a different account, please logout first", user.Name)
77+
return nil
6278
}
63-
f.Log.Debugw("Already logged in", "token string", f.Config.GetTokenString(),
64-
"token detail", f.Config.GetToken(), "user", user)
65-
f.Log.Infof("Already logged in as %s, "+
66-
"if you want to use a different account, please logout first", user.Name)
67-
return nil
6879
}
6980

7081
var (

0 commit comments

Comments
 (0)