From 741110208df7e1eff6967991693a62e7d0eb8fd2 Mon Sep 17 00:00:00 2001 From: MattEqualsCoder Date: Mon, 5 Oct 2026 21:11:46 -0400 Subject: [PATCH] Update to immediately save twitch login info --- .../TwitchAuthenticationService.cs | 11 ++- src/TrackerCouncil.Smz3.Tracking/Tracker.cs | 5 ++ .../Services/MainWindowService.cs | 1 + .../Services/OptionsWindowService.cs | 81 ++++++++++++++----- 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchAuthenticationService.cs b/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchAuthenticationService.cs index 23086c57b..0fd166da9 100644 --- a/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchAuthenticationService.cs +++ b/src/TrackerCouncil.Smz3.Chat.Twitch/TwitchAuthenticationService.cs @@ -42,10 +42,19 @@ public override async Task ValidateTokenAsync(string accessToken, Cancella var response = await s_httpClient.SendAsync(request, cancellationToken); if (response.IsSuccessStatusCode) { - using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken); + await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken); var validateResponse = await JsonSerializer.DeserializeAsync(responseStream, cancellationToken: cancellationToken); if (validateResponse?.ExpiresIn > 0) + { + logger.LogInformation("Successfully validated Twitch OAuth token"); return true; + } + + logger.LogWarning("Twitch OAuth token expired"); + } + else + { + logger.LogError("Unsuccessful API status code {Code} when checking Twitch OAuth token validity.", response.StatusCode); } } catch (Exception ex) diff --git a/src/TrackerCouncil.Smz3.Tracking/Tracker.cs b/src/TrackerCouncil.Smz3.Tracking/Tracker.cs index 4488c89ae..839362ca4 100644 --- a/src/TrackerCouncil.Smz3.Tracking/Tracker.cs +++ b/src/TrackerCouncil.Smz3.Tracking/Tracker.cs @@ -353,6 +353,7 @@ public override async Task ConnectToChatAsync(string? userName, string? oauthTok { try { + _logger.LogInformation("Connecting to Twitch Chat"); await _chatClient.ConnectAsync(userName, oauthToken, channel ?? userName, id ?? ""); } catch (AggregateException e) @@ -361,6 +362,10 @@ public override async Task ConnectToChatAsync(string? userName, string? oauthTok Say(x => x.Chat.WhenDisconnected); } } + else if (!string.IsNullOrEmpty(userName)) + { + _logger.LogWarning("Twitch username specified, but no auth token found"); + } } /// diff --git a/src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs b/src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs index bdc823e3e..597b9f9d4 100644 --- a/src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs +++ b/src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs @@ -92,6 +92,7 @@ public async Task ValidateTwitchToken() if (!isTokenValid) { + logger.LogInformation("Clearing Twitch oauth token due to it being invalid"); _options.GeneralOptions.TwitchOAuthToken = string.Empty; Dispatcher.UIThread.Invoke(() => { diff --git a/src/TrackerCouncil.Smz3.UI/Services/OptionsWindowService.cs b/src/TrackerCouncil.Smz3.UI/Services/OptionsWindowService.cs index c795bc613..85e393594 100644 --- a/src/TrackerCouncil.Smz3.UI/Services/OptionsWindowService.cs +++ b/src/TrackerCouncil.Smz3.UI/Services/OptionsWindowService.cs @@ -108,15 +108,25 @@ public void Close() public void SaveViewModel() { - var options = optionsFactory.Create(); - _model.UpdateOptions(options.GeneralOptions); + logger.LogInformation("Saving option window settings"); - if (!Directory.Exists(options.GeneralOptions.RomOutputPath)) + try { - Directory.CreateDirectory(options.GeneralOptions.RomOutputPath); + var options = optionsFactory.Create(); + _model.UpdateOptions(options.GeneralOptions); + + if (!Directory.Exists(options.GeneralOptions.RomOutputPath)) + { + Directory.CreateDirectory(options.GeneralOptions.RomOutputPath); + } + + options.Save(); + } + catch (Exception e) + { + logger.LogError(e, "Error saving options window settings"); } - options.Save(); } private async Task ValidateTwitchOAuthToken() @@ -150,13 +160,16 @@ await Task.Run(async () => { try { + logger.LogInformation("Attempting to log into twitch"); + var token = await chatAuthenticationService.GetTokenInteractivelyAsync(default); - if(token == null) + if (string.IsNullOrEmpty(token)) { logger.LogError("Token returned by chat authentication service null"); - TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " + - "Please try again or report this issue with the log file.")); + TwitchError?.Invoke(this, new TwitchErrorEventHandler( + "An unexpected error occurred while trying to log you in with Twitch. " + + "Please try again or report this issue with the log file.")); return; } @@ -165,8 +178,9 @@ await Task.Run(async () => if (userData == null) { logger.LogError("User Data returned by chat authentication service null"); - TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " + - "Please try again or report this issue with the log file.")); + TwitchError?.Invoke(this, new TwitchErrorEventHandler( + "An unexpected error occurred while trying to log you in with Twitch. " + + "Please try again or report this issue with the log file.")); return; } @@ -174,12 +188,15 @@ await Task.Run(async () => _model.TwitchIntegration.TwitchOAuthToken = token; _model.TwitchIntegration.TwitchChannel = userData.Name; _model.TwitchIntegration.TwitchId = userData.Id; + SaveTwitchLogin(); + logger.LogInformation("Updated Twitch token successfully"); } catch (Exception ex) { logger.LogError(ex, "An unknown error occurred while logging in with Twitch"); - TwitchError?.Invoke(this, new TwitchErrorEventHandler("An unexpected error occurred while trying to log you in with Twitch. " + - "Please try again or report this issue with the log file.")); + TwitchError?.Invoke(this, new TwitchErrorEventHandler( + "An unexpected error occurred while trying to log you in with Twitch. " + + "Please try again or report this issue with the log file.")); } }); } @@ -193,21 +210,41 @@ await Task.Run(async () => private async void TwitchLogout() { - if (string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken)) - return; + try + { + if (string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken)) + return; + + var revoked = await chatAuthenticationService.RevokeTokenAsync(_model.TwitchIntegration.TwitchOAuthToken, default); - var revoked = await chatAuthenticationService.RevokeTokenAsync(_model.TwitchIntegration.TwitchOAuthToken, default); + if (revoked) + { + _model.TwitchIntegration.TwitchUserName = ""; + _model.TwitchIntegration.TwitchOAuthToken = ""; + _model.TwitchIntegration.TwitchChannel = ""; + _model.TwitchIntegration.TwitchId = ""; + } - if (revoked) + _model.TwitchIntegration.TwitchStatusText = revoked ? "Logged out." : "Something went wrong."; + _model.TwitchIntegration.IsLoggedIn = false; + SaveTwitchLogin(); + } + catch (Exception e) { - _model.TwitchIntegration.TwitchUserName = ""; - _model.TwitchIntegration.TwitchOAuthToken = ""; - _model.TwitchIntegration.TwitchChannel = ""; - _model.TwitchIntegration.TwitchId = ""; + logger.LogError(e, "Error logging out from Twitch"); } - _model.TwitchIntegration.TwitchStatusText = revoked ? "Logged out." : "Something went wrong."; - _model.TwitchIntegration.IsLoggedIn = false; + } + + private void SaveTwitchLogin() + { + logger.LogInformation("Saving Twitch login settings. Valid OAuthToken? {Value}", !string.IsNullOrEmpty(_model.TwitchIntegration.TwitchOAuthToken)); + var options = optionsFactory.Create(); + options.GeneralOptions.TwitchUserName = _model.TwitchIntegration.TwitchUserName; + options.GeneralOptions.TwitchOAuthToken = _model.TwitchIntegration.TwitchOAuthToken; + options.GeneralOptions.TwitchChannel = _model.TwitchIntegration.TwitchChannel; + options.GeneralOptions.TwitchId = _model.TwitchIntegration.TwitchId; + options.Save(); } private async Task UpdateConfigsAsync()