Skip to content

Commit

Permalink
[auth] change GetSession so it returns ok (#158)
Browse files Browse the repository at this point in the history
## Summary

This will make code more readable and reduce mistakes when using
library. An invalid token is not super useful to client.

Updated error messages for new interface.

I still think an error is better (can help with refresh failures, etc)
but @loreto you mentioned preferring this.

## How was it tested?

Untested.
  • Loading branch information
mikeland73 authored Sep 21, 2023
1 parent f7f9113 commit 63f6624
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions internal/envcli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package envcli
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec/internal/envvar"
"go.jetpack.io/pkg/sandbox/auth"
Expand Down Expand Up @@ -101,10 +102,9 @@ func whoAmICmd() *cobra.Command {
return err
}

tok := client.GetSession()
if tok == nil {
fmt.Fprintln(cmd.OutOrStdout(), "Not logged in")
return nil
tok, ok := client.GetSession()
if !ok {
return errors.New("not logged in. Run `envsec auth login` to log in")
}
idClaims := tok.IDClaims()

Expand Down
5 changes: 3 additions & 2 deletions internal/envcli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type cmdConfig struct {

func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
var tok *session.Token
var ok bool
var err error

if f.orgID == "" {
Expand All @@ -89,8 +90,8 @@ func (f *configFlags) genConfig(ctx context.Context) (*cmdConfig, error) {
return nil, err
}

tok = client.GetSession()
if tok == nil {
tok, ok = client.GetSession()
if !ok {
return nil, errors.Errorf(
"To use envsec you must log in (`envsec auth login`) or specify --project-id and --org-id",
)
Expand Down
5 changes: 4 additions & 1 deletion internal/envcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ func initCmd() *cobra.Command {
if err != nil {
return err
}
tok := client.GetSession()
tok, ok := client.GetSession()
if !ok {
return errors.New("not logged in, run `envsec auth login`")
}

wd, err := os.Getwd()
if err != nil {
Expand Down

0 comments on commit 63f6624

Please sign in to comment.