AlexxIT/go2rtc · error

wrong login step

Error message

wrong login step

What it means

Xiaomi cloud login is a multi-step state machine. LoginWithCaptcha requires that an earlier step (LoginStep) has already stored the "ick" cookie in c.auth. If auth state is missing or "ick" is empty, the method panics because a captcha cannot be submitted without the prior response context.

Solutions

  1. Call the initial login step (c.LoginStep(...)) first so c.auth gets populated with the "ick" value, then call LoginWithCaptcha
  2. Reuse the same Cloud instance across the whole login flow instead of creating a new one per step
  3. Replace the panic with a returned error in a wrapper if you want graceful handling in your own code

Example fix

// before
cloud := &xiaomi.Cloud{}
cloud.LoginWithCaptcha("1234")
// after
cloud := &xiaomi.Cloud{}
if err := cloud.LoginStep(user, pass); err != nil { return err }
if err := cloud.LoginWithCaptcha("1234"); err != nil { return err }
Defensive patterns

Strategy: validation

Validate before calling

if cloud.Auth == nil || cloud.Auth["ick"] == "" {
    return errors.New("must call login step before LoginWithCaptcha")
}

Prevention

When it happens

Trigger: Calling Cloud.LoginWithCaptcha before calling the initial LoginStep, calling it twice, or calling it on a fresh Cloud instance.

Common situations: Developers scripting the Xiaomi Mi Home cloud login out of order (captcha flow requires: login step -> captcha -> verify); retrying captcha submission after process restart loses auth state.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/07036c72767059c2. Report an issue: GitHub.

Appendix: source

Thrown at pkg/xiaomi/cloud.go:132

	if v2.NotificationURL != "" {
		return c.authStart(v2.NotificationURL)
	}

	if v2.Location == "" {
		return fmt.Errorf("xiaomi: %s", body)
	}

	c.auth = nil
	c.ssecurity = v2.Ssecurity
	c.passToken = v2.PassToken

	return c.finishAuth(v2.Location)
}

func (c *Cloud) LoginWithCaptcha(captcha string) error {
	if c.auth == nil || c.auth["ick"] == "" {
		panic("wrong login step")
	}

	c.auth["captcha_code"] = captcha

	// check if captcha after verify
	if c.auth["flag"] != "" {
		return c.sendTicket()
	}

	return c.Login(c.auth["username"], c.auth["password"])
}

func (c *Cloud) LoginWithVerify(ticket string) error {
	if c.auth == nil || c.auth["flag"] == "" {
		panic("wrong login step")
	}

	req := Request{

View on GitHub (pinned to c245815e75)