Automatically clear the jar and delete the cookie file for re-auth;

if the Client ID within the cookie does not match the Client ID currently used by the application (#107)
This commit is contained in:
DevilXD committed 2024-09-15 14:25:20 +02:00
1 parent 77e2ce0c28
commit df05fb2707
1 file changed
+12 -4
+12 -4
View File
@@ -374,7 +374,8 @@ class _AuthState:
login_form: LoginForm = self._twitch.gui.login
logger.info("Checking login")
login_form.update(_("gui", "login", "logging_in"), None)
for attempt in range(2):
for client_mismatch_attempt in range(2):
for invalid_token_attempt in range(2):
cookie = jar.filter_cookies(client_info.CLIENT_URL)
if "auth-token" not in cookie:
self.access_token = await self._oauth_login()
@@ -399,9 +400,16 @@ class _AuthState:
validate_response = await response.json()
break
else:
raise RuntimeError("Login verification failure")
if validate_response["client_id"] != client_info.CLIENT_ID:
raise LoginException("You're using an old cookie file, please generate a new one.")
raise RuntimeError("Login verification failure (step #2)")
# ensure the cookie's client ID matches the currently selected client
if validate_response["client_id"] == client_info.CLIENT_ID:
break
# otherwise, we need to delete the entire cookie file and clear the jar
logger.info("Cookie client ID mismatch")
jar.clear()
COOKIES_PATH.unlink(missing_ok=True)
else:
raise RuntimeError("Login verification failure (step #1)")
self.user_id = int(validate_response["user_id"])
cookie["persistent"] = str(self.user_id)
logger.info(f"Login successful, user ID: {self.user_id}")