Skip to content

Commit eb690d4

Browse files
committed
fix: fix init auth
1 parent 0da6308 commit eb690d4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pkg/cmd/auth.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ func authenticate(ctx context.Context, cmd *cli.Command, forceAuthentication boo
7070
}
7171
}
7272

73+
group := console.Info("To authenticate, visit the verification URL")
7374
cc := getAPICommandContext(cmd)
7475
clientID := cmd.String("client-id")
7576
scope := "*"
76-
authResult, err := startDeviceFlow(ctx, cmd, cc.client, clientID, scope)
77+
authResult, err := startDeviceFlow(ctx, cmd, cc.client, clientID, scope, group)
7778
if err != nil {
7879
return err
7980
}
@@ -86,7 +87,7 @@ func authenticate(ctx context.Context, cmd *cli.Command, forceAuthentication boo
8687
console.Error("Failed to save authentication: %v", err)
8788
return fmt.Errorf("authentication failed")
8889
}
89-
console.Success("Authentication successful! Your credentials have been saved to %s", config.ConfigPath)
90+
group.Success("Authentication successful! Your credentials have been saved to %s", config.ConfigPath)
9091
return nil
9192
}
9293

@@ -146,7 +147,7 @@ func handleAuthStatus(ctx context.Context, cmd *cli.Command) error {
146147
}
147148

148149
// startDeviceFlow initiates the OAuth 2.0 device flow
149-
func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Client, clientID, scope string) (*AuthConfig, error) {
150+
func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Client, clientID, scope string, group console.Group) (*AuthConfig, error) {
150151
var deviceResponse struct {
151152
DeviceCode string `json:"device_code"`
152153
UserCode string `json:"user_code"`
@@ -156,6 +157,10 @@ func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Cli
156157
Interval int `json:"interval"`
157158
}
158159

160+
if clientID == "" {
161+
clientID = defaultClientID
162+
}
163+
159164
err := client.Post(ctx, "api/oauth/device", map[string]string{
160165
"client_id": clientID,
161166
"scope": scope,
@@ -166,7 +171,6 @@ func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Cli
166171
return nil, err
167172
}
168173

169-
group := console.Info("To authenticate, visit the verification URL")
170174
group.Property("url", deviceResponse.VerificationURIComplete)
171175
group.Property("code", deviceResponse.UserCode)
172176

@@ -189,15 +193,16 @@ func startDeviceFlow(ctx context.Context, cmd *cli.Command, client stainless.Cli
189193
// Hard-code to 1 second for now, instead of using deviceResponse.Interval
190194
1,
191195
deviceResponse.ExpiresIn,
196+
group,
192197
)
193198
}
194199

195200
// pollForToken polls the token endpoint until the user completes authentication
196-
func pollForToken(ctx context.Context, client stainless.Client, clientID, deviceCode string, interval, expiresIn int) (*AuthConfig, error) {
201+
func pollForToken(ctx context.Context, client stainless.Client, clientID, deviceCode string, interval, expiresIn int, group console.Group) (*AuthConfig, error) {
197202
deadline := time.Now().Add(time.Duration(expiresIn) * time.Second)
198203
pollInterval := time.Duration(interval) * time.Second
199204

200-
console.Progress("Waiting for authentication to complete...")
205+
group.Progress("Waiting for authentication to complete...")
201206

202207
for time.Now().Before(deadline) {
203208
time.Sleep(pollInterval)

0 commit comments

Comments
 (0)