Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/app/onboarding/preflight/preflight_effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import (

tea "charm.land/bubbletea/v2"

iauth "github.com/usetero/cli/internal/auth"
"github.com/usetero/cli/internal/core/bootstrap"
"github.com/usetero/cli/internal/domain"
)

func (m *Model) checkAuth() tea.Cmd {
return func() tea.Msg {
hasValidAuth := false
var user *iauth.User
if m.auth.IsAuthenticated() {
if _, err := m.auth.GetAccessToken(m.ctx); err == nil {
hasValidAuth = true
if userID, err := m.auth.GetUserID(m.ctx); err == nil && userID != "" {
user = &iauth.User{ID: userID}
}
} else {
_ = m.auth.ClearTokens()
}
}
return preflightAuthCheckCompletedMsg{hasValidAuth: hasValidAuth}
return preflightAuthCheckCompletedMsg{hasValidAuth: hasValidAuth, user: user}
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/app/onboarding/preflight/preflight_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package preflight

import (
"github.com/usetero/cli/internal/auth"
"github.com/usetero/cli/internal/core/bootstrap"
"github.com/usetero/cli/internal/domain"
)
Expand All @@ -11,6 +12,7 @@ type preflightResolutionCompletedMsg struct {

type preflightAuthCheckCompletedMsg struct {
hasValidAuth bool
user *auth.User
}

type preflightOrganizationsLoadedMsg struct {
Expand Down
1 change: 1 addition & 0 deletions internal/app/onboarding/preflight/preflight_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {

func (m *Model) handleAuthChecked(msg preflightAuthCheckCompletedMsg) tea.Cmd {
m.state.HasValidAuth = msg.hasValidAuth
m.state.User = msg.user
if !m.state.HasValidAuth {
return m.emitResult()
}
Expand Down
21 changes: 21 additions & 0 deletions internal/app/onboarding/transitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ func TestHandleTransitionPreflightRouting(t *testing.T) {
}
}

func TestHandleTransitionPreflightResolvedCarriesUserState(t *testing.T) {
t.Parallel()

m := newTestModel(t)
user := ptrUser("user-1")

cmd := m.handleTransition(bootstrap.PreflightResolved{State: bootstrap.PreflightState{
HasValidAuth: true,
User: user,
Role: bootstrap.RolePlatform,
Org: ptrOrg("org-1"),
Account: ptrAccount("acc-1"),
}})
if cmd == nil {
t.Fatal("expected command")
}
if m.state.User == nil || m.state.User.ID != user.ID {
t.Fatalf("user state = %+v, want %s", m.state.User, user.ID)
}
}

func TestHandleTransitionDatadogBranchRouting(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions internal/core/bootstrap/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type OnboardingComplete struct {
type PreflightState struct {
Outcome PreflightOutcome
HasValidAuth bool
User *auth.User
Role string
ActiveOrgID domain.OrganizationID
DefaultAccountID domain.AccountID
Expand Down
3 changes: 3 additions & 0 deletions internal/core/bootstrap/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func ApplyPreflight(state State, resolved PreflightState) (State, Gate) {
if resolved.Account != nil {
state.Account = resolved.Account
}
if resolved.User != nil {
state.User = resolved.User
}

return state, next
}
Expand Down
Loading