From b104f98d02ea2ebf7560ca6bf6a90c36ed670088 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Fri, 25 Sep 2026 19:43:24 +0200 Subject: [PATCH] Fix Twitch token status not reflecting reality when cached user ID empty --- plugins/twitch/token.cpp | 32 +++++++++++++++++++++++--------- plugins/twitch/token.hpp | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/plugins/twitch/token.cpp b/plugins/twitch/token.cpp index 662e59a7..f0a82e7b 100644 --- a/plugins/twitch/token.cpp +++ b/plugins/twitch/token.cpp @@ -233,7 +233,8 @@ void TwitchToken::Load(obs_data_t *obj) { Item::Load(obj); _token = obs_data_get_string(obj, "token"); - if (obs_data_has_user_value(obj, "userID")) { + if (obs_data_has_user_value(obj, "userID") && + *obs_data_get_string(obj, "userID") != '\0') { _userID = obs_data_get_string(obj, "userID"); } else { _userID = {}; @@ -259,7 +260,7 @@ void TwitchToken::Save(obs_data_t *obj) const { Item::Save(obj); obs_data_set_string(obj, "token", _token.c_str()); - if (_userID) { + if (_userID && !_userID->empty()) { obs_data_set_string(obj, "userID", _userID->c_str()); } obs_data_set_bool(obj, "validateEventSubTimestamps", @@ -314,7 +315,7 @@ bool TwitchToken::AnyOptionIsEnabled( return false; } -void TwitchToken::SetToken(const std::string &value) +bool TwitchToken::SetToken(const std::string &value) { _token = value; auto res = @@ -322,21 +323,34 @@ void TwitchToken::SetToken(const std::string &value) if (res.status != 200) { blog(LOG_WARNING, "failed to get Twitch user id from token!"); _userID = {}; - return; + return false; } + bool gotUserID = false; OBSDataArrayAutoRelease array = obs_data_get_array(res.data, "data"); size_t count = obs_data_array_count(array); for (size_t i = 0; i < count; i++) { OBSDataAutoRelease arrayObj = obs_data_array_item(array, i); - _userID = obs_data_get_string(arrayObj, "id"); + std::string id = obs_data_get_string(arrayObj, "id"); + if (id.empty()) { + blog(LOG_WARNING, + "Twitch /helix/users response did not contain a user id!"); + continue; + } + _userID = id; _name = obs_data_get_string(arrayObj, "display_name"); + gotUserID = true; + } + + if (!gotUserID) { + return false; } // Trigger resubscribes with new token if (_eventSub) { _eventSub->ClearActiveSubscriptions(); } + return true; } std::optional TwitchToken::GetToken() const @@ -392,7 +406,7 @@ bool TwitchToken::IsValid(bool forceUpdate) const return false; } - if (_userID && _userID != id) { + if (_userID && !_userID->empty() && _userID != id) { blog(LOG_INFO, "Twitch token %s does not match expected user (got %s, expected %s)!", _name.c_str(), id, _userID->c_str()); @@ -400,7 +414,7 @@ bool TwitchToken::IsValid(bool forceUpdate) const return false; } - if (!_userID) { + if (!_userID || _userID->empty()) { _userID = id; } @@ -827,9 +841,9 @@ void TwitchTokenSettingsDialog::GotToken(const std::optional &value) return; } - _currentToken.SetToken(value.value().toStdString()); + bool success = _currentToken.SetToken(value.value().toStdString()); auto name = QString::fromStdString(_currentToken._name); - if (name.isEmpty()) { + if (!success || name.isEmpty()) { _tokenStatus->setText(obs_module_text( "AdvSceneSwitcher.twitchToken.request.fail")); _name->setText(""); diff --git a/plugins/twitch/token.hpp b/plugins/twitch/token.hpp index 8c1301e1..17aada14 100644 --- a/plugins/twitch/token.hpp +++ b/plugins/twitch/token.hpp @@ -48,7 +48,7 @@ public: bool OptionIsActive(const TokenOption &option) const; bool OptionIsEnabled(const TokenOption &option) const; bool AnyOptionIsEnabled(const std::vector &options) const; - void SetToken(const std::string &); + bool SetToken(const std::string &); bool IsEmpty() const { return _token.empty(); } std::optional GetToken() const; std::optional GetUserID() const { return _userID; }