MCPcopy Create free account
hub / github.com/Thunder-Compute/thunder-cli / runInteractiveLogin

Function runInteractiveLogin

cmd/login.go:323–410  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

321}
322
323func runInteractiveLogin() error {
324 state, err := generateState()
325 if err != nil {
326 return fmt.Errorf("failed to generate state: %w", err)
327 }
328
329 ctx, cancel := context.WithCancel(context.Background())
330 defer cancel()
331
332 port, authChan, errChan, cleanup, err := startCallbackServerWithContext(ctx)
333 if err != nil {
334 return fmt.Errorf("failed to start callback server: %w", err)
335 }
336 defer cleanup()
337
338 returnURI := fmt.Sprintf("%s:%d/callback", callbackURL, port)
339 authURLWithParams := buildAuthURL(state, returnURI)
340
341 model := tui.NewLoginModel(authURLWithParams)
342 p := tea.NewProgram(model, tea.WithAltScreen())
343
344 go func() {
345 select {
346 case authResp := <-authChan:
347 tui.SendLoginSuccess(p, authResp.Token)
348 case err := <-errChan:
349 tui.SendLoginError(p, err)
350 case <-ctx.Done():
351 tui.SendLoginCancel(p)
352 case <-time.After(5 * time.Minute):
353 tui.SendLoginError(p, fmt.Errorf("authentication timeout - if you're using SSH, copy the token from your browser and press 'T' to enter it manually"))
354 }
355 }()
356
357 if err := openBrowser(authURLWithParams); err != nil {
358 fmt.Printf("Failed to open browser automatically: %v\n", err)
359 }
360
361 finalModel, err := p.Run()
362 if err != nil {
363 if fm, ok := finalModel.(tui.LoginModel); ok {
364 model = fm
365 }
366 if model.State() == tui.LoginStateCancelled {
367 PrintWarningSimple("User cancelled authentication")
368 return nil
369 }
370 return fmt.Errorf("TUI error: %w", err)
371 }
372
373 // Update model from the returned final state. The original model variable
374 // is a value copy that bubbletea never mutates — the real final state is
375 // only available from the p.Run() return value.
376 if fm, ok := finalModel.(tui.LoginModel); ok {
377 model = fm
378 }
379
380 if model.State() == tui.LoginStateSuccess {

Callers 1

runLoginFunction · 0.85

Calls 15

StateMethod · 0.95
TokenMethod · 0.95
ValidateTokenMethod · 0.95
ErrorMethod · 0.95
NewLoginModelFunction · 0.92
SendLoginSuccessFunction · 0.92
SendLoginErrorFunction · 0.92
SendLoginCancelFunction · 0.92
NewClientFunction · 0.92
generateStateFunction · 0.85
buildAuthURLFunction · 0.85

Tested by

no test coverage detected