Skip to content

Commit

Permalink
fix(auth/login): zeabur auth login failed if the token has expired (#95)
Browse files Browse the repository at this point in the history
<!-- 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
  • Loading branch information
MichaelYuhe authored Mar 25, 2024
2 parents 61f7a7f + 7ed0eac commit 3ea07b0
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 3ea07b0

Please sign in to comment.