Achievements: Log-in explicitly on startup to refresh the token

This commit is contained in:
Silent 2023-07-26 23:41:08 +02:00 committed by Connor McLaughlin
parent 1c39ed6130
commit 5b486ca6b8
3 changed files with 50 additions and 10 deletions

View File

@ -106,7 +106,8 @@ namespace Achievements
static void EndLoadingScreen(bool was_running_idle);
static void LoginCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data);
static void LoginASyncCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data);
static void SendLogin(const char* username, const char* password, Common::HTTPDownloader* http_downloader,
static void LoginWithTokenCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data);
static void SendLogin(const char* username, const char* password, const char* api_token, Common::HTTPDownloader* http_downloader,
Common::HTTPDownloader::Request::Callback callback);
static void DownloadImage(std::string url, std::string cache_filename);
static void DisplayAchievementSummary();
@ -445,6 +446,11 @@ bool Achievements::IsLoggedIn()
return s_logged_in;
}
bool Achievements::HasSavedCredentials()
{
return !s_username.empty() && !s_api_token.empty();
}
bool Achievements::ChallengeModeActive()
{
#ifdef ENABLE_RAINTEGRATION
@ -513,7 +519,7 @@ void Achievements::Initialize()
s_last_ping_time.Reset();
s_username = Host::GetBaseStringSettingValue("Achievements", "Username");
s_api_token = Host::GetBaseStringSettingValue("Achievements", "Token");
s_logged_in = (!s_username.empty() && !s_api_token.empty());
s_logged_in = false; // We are not logged in until we confirm those credentials
if (VMManager::HasValidVM())
GameChanged(VMManager::GetDiscCRC(), VMManager::GetCurrentCRC());
@ -953,13 +959,34 @@ void Achievements::LoginASyncCallback(s32 status_code, const std::string& conten
LoginCallback(status_code, std::move(content_type), std::move(data));
}
void Achievements::LoginWithTokenCallback(s32 status_code, const std::string& content_type, Common::HTTPDownloader::Request::Data data)
{
std::unique_lock lock(s_achievements_mutex);
RAPIResponse<rc_api_login_response_t, rc_api_process_login_response, rc_api_destroy_login_response> response(status_code, data);
if (!response)
{
FormattedError("Login failed. Please check your user name and password, and try again.");
return;
}
if (s_active)
{
s_logged_in = true;
// If we have a game running, set it up.
if (!s_game_hash.empty())
SendGetGameId();
}
}
void Achievements::SendLogin(
const char* username, const char* password, Common::HTTPDownloader* http_downloader, Common::HTTPDownloader::Request::Callback callback)
const char* username, const char* password, const char* api_token, Common::HTTPDownloader* http_downloader, Common::HTTPDownloader::Request::Callback callback)
{
RAPIRequest<rc_api_login_request_t, rc_api_init_login_request> request;
request.username = username;
request.password = password;
request.api_token = nullptr;
request.api_token = api_token;
request.Send(http_downloader, std::move(callback));
}
@ -975,7 +1002,7 @@ bool Achievements::LoginAsync(const char* username, const char* password)
ImGuiFullscreen::OpenBackgroundProgressDialog("cheevos_async_login", "Logging in to RetroAchievements...", 0, 0, 0);
}
SendLogin(username, password, s_http_downloader.get(), LoginASyncCallback);
SendLogin(username, password, nullptr, s_http_downloader.get(), LoginASyncCallback);
return true;
}
@ -989,7 +1016,7 @@ bool Achievements::Login(const char* username, const char* password)
if (s_active)
{
SendLogin(username, password, s_http_downloader.get(), LoginCallback);
SendLogin(username, password, nullptr, s_http_downloader.get(), LoginCallback);
s_http_downloader->WaitForAllRequests();
return IsLoggedIn();
}
@ -1000,12 +1027,21 @@ bool Achievements::Login(const char* username, const char* password)
if (!http_downloader)
return false;
SendLogin(username, password, http_downloader.get(), LoginCallback);
SendLogin(username, password, nullptr, http_downloader.get(), LoginCallback);
http_downloader->WaitForAllRequests();
return !Host::GetBaseStringSettingValue("Achievements", "Token").empty();
}
bool Achievements::LoginWithTokenAsync(const char* username, const char* api_token)
{
if (s_logged_in || std::strlen(username) == 0 || std::strlen(api_token) == 0 || IsUsingRAIntegration())
return false;
SendLogin(username, nullptr, api_token, s_http_downloader.get(), LoginWithTokenCallback);
return true;
}
void Achievements::Logout()
{
if (s_active)
@ -1015,11 +1051,11 @@ void Achievements::Logout()
if (s_logged_in)
{
ClearGameInfo();
std::string().swap(s_username);
std::string().swap(s_api_token);
s_logged_in = false;
Host::OnAchievementsRefreshed();
}
std::string().swap(s_username);
std::string().swap(s_api_token);
}
// remove from config
@ -1477,6 +1513,8 @@ void Achievements::GameChanged(u32 disc_crc, u32 crc)
if (IsLoggedIn())
SendGetGameId();
else if (HasSavedCredentials())
LoginWithTokenAsync(s_username.c_str(), s_api_token.c_str());
}
void Achievements::SendGetGameId()

View File

@ -88,6 +88,7 @@ namespace Achievements
bool IsActive();
bool IsLoggedIn();
bool HasSavedCredentials();
bool ChallengeModeActive();
bool LeaderboardsActive();
bool IsTestModeActive();
@ -131,6 +132,7 @@ namespace Achievements
bool LoginAsync(const char* username, const char* password);
bool Login(const char* username, const char* password);
bool LoginWithTokenAsync(const char* username, const char* api_token);
void Logout();
void GameChanged(u32 disc_crc, u32 crc);

View File

@ -6990,7 +6990,7 @@ void FullscreenUI::DrawAchievementsSettingsPage(std::unique_lock<std::mutex>& se
Achievements::ProcessPendingHTTPRequestsFromGSThread();
MenuHeading("Account");
if (Achievements::IsLoggedIn())
if (Achievements::HasSavedCredentials())
{
ImGui::PushStyleColor(ImGuiCol_TextDisabled, ImGui::GetStyle().Colors[ImGuiCol_Text]);
ActiveButton(fmt::format(ICON_FA_USER " Username: {}", Achievements::GetUsername()).c_str(), false, false,