Every time pm2 restarts, the Zerodha token may be in EXPIRED state
(knocked out by the engine between midnight and 6:05 AM auto-login).
Now on startup we check each auto-login user's broker auth_state and
immediately re-login anyone who is expired/disconnected, so the broker
shows as connected the moment the user opens the website.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Frontend checks authState === 'VALID' to clear the session-expired
banner. Using 'CONNECTED' left the banner stuck even after a successful
token refresh.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
After twofa verifies identity, re-visit connect/login as authenticated user.
Zerodha then redirects to the registered callback URL with request_token.
This mirrors what the browser JS does to complete the OAuth flow.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The sess_id from the connect/login redirect is Zerodha's OAuth session
identifier. Without it in the twofa POST, Zerodha returns profile:{}
(regular web login) instead of redirect_url with request_token.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Following the connect/login redirect chain overwrites kf_session with a
plain web session, stripping the OAuth context. Stop at the first 302
so kf_session retains the api_key OAuth context through the twofa step.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Referer on POST was causing Zerodha to reject login with Invalid username.
Add Accept, Accept-Language, Origin headers for proper browser-like requests.
Keep redirect_url body check for twofa. Add login step debug logging.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Root cause: X-Kite-Version header on web login endpoints caused Zerodha
to return plain profile response instead of OAuth redirect_url.
Changes:
- Remove X-Kite-Version from session headers (only valid for Kite Connect API)
- Use allow_redirects=False on connect/login GET to preserve OAuth cookie
- Add Referer header to login/twofa POSTs
- Check data.redirect_url in twofa JSON body (modern Zerodha SPA behavior)
- Keep Location header fallback for legacy behavior
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Prints twofa response status, Location header, and body so we can see
exactly what Zerodha returns after TOTP submission.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Without first GETting the connect/login URL with the api_key,
Zerodha doesn't know which app is logging in and never returns
a request_token after TOTP — causing the redirect loop to fail.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- New auto_login_service.py: stores encrypted credentials (login ID,
password, TOTP secret), performs headless Zerodha login via pyotp,
and refreshes the session daily at 6:05 AM IST via background thread
- New auto_login router: setup, status, remove, and manual trigger endpoints
- Scheduler started at app boot alongside existing daemons
- Added pyotp==2.9.0 dependency
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>