1818
1919#include " CustomAuthStep.h"
2020
21+ #include < QInputDialog>
22+
2123#include " Application.h"
2224#include " Logging.h"
2325#include " net/NetUtils.h"
@@ -77,7 +79,11 @@ QString CustomAuthStep::requestTemplate()
7779{
7880 "accessToken": "%1",
7981 "clientToken": "%2",
80- "requestUser": false
82+ "requestUser": false,
83+ "selectedProfile": {
84+ "id": "%3",
85+ "name": "%4"
86+ }
8187}
8288)XXX" ;
8389 }
@@ -88,7 +94,8 @@ QString CustomAuthStep::fillRequest()
8894 if (m_action == AuthFlow::Action::Login) {
8995 return requestTemplate ().arg (m_data->accountLogin , m_password, clientID ());
9096 } else {
91- return requestTemplate ().arg (m_data->yggdrasilToken .token , m_data->clientID );
97+ return requestTemplate ().arg (m_data->yggdrasilToken .token , m_data->clientID , m_data->minecraftProfile .id ,
98+ m_data->minecraftProfile .name );
9299 }
93100}
94101
@@ -106,9 +113,43 @@ bool CustomAuthStep::parseResponse()
106113
107114 m_data->clientID = jsonResponse[" clientToken" ].toString ();
108115
109- auto profile = jsonResponse[" selectedProfile" ].toObject ();
110- m_data->minecraftProfile .id = profile[" id" ].toString ();
111- m_data->minecraftProfile .name = profile[" name" ].toString ();
116+ if (!jsonResponse[" selectedProfile" ].isNull ()) {
117+ auto profile = jsonResponse[" selectedProfile" ].toObject ();
118+ m_data->minecraftProfile .id = profile[" id" ].toString ();
119+ m_data->minecraftProfile .name = profile[" name" ].toString ();
120+ }
121+
122+ const QJsonArray profiles = jsonResponse[" availableProfiles" ].toArray ();
123+ if (profiles.size () > 1 ) {
124+ const auto profileName = [](const auto & profile) {
125+ auto obj = profile.toObject ();
126+ return obj[" name" ].toString ();
127+ };
128+
129+ QStringList list;
130+ std::ranges::transform (profiles, std::back_inserter (list), profileName);
131+
132+ bool ok = false ;
133+ QString selectedProfileName =
134+ QInputDialog::getItem (nullptr , tr (" Select profile" ), tr (" Select profile for this account" ), list, 0 , false , &ok);
135+
136+ if (!ok) {
137+ return false ;
138+ }
139+
140+ const auto it = std::ranges::find (profiles, selectedProfileName, profileName);
141+ if (it != profiles.end ()) {
142+ auto profileObj = it->toObject ();
143+ m_data->minecraftProfile = MinecraftProfile{ .id = profileObj[" id" ].toString (), .name = profileObj[" name" ].toString () };
144+ } else {
145+ return false ;
146+ }
147+ }
148+
149+ if (profiles.size () == 1 && m_data->minecraftProfile .id .isEmpty ()) {
150+ auto profileObj = profiles.first ().toObject ();
151+ m_data->minecraftProfile = MinecraftProfile{ .id = profileObj[" id" ].toString (), .name = profileObj[" name" ].toString () };
152+ }
112153
113154 return true ;
114155}
@@ -121,4 +162,4 @@ void CustomAuthStep::onRequestDone()
121162 return ;
122163 }
123164 emit finished (AccountTaskState::STATE_WORKING , tr (" Got authorization for %1 account" ).arg (authType ()));
124- }
165+ }
0 commit comments