Skip to content

Commit 81fa225

Browse files
committed
Redirect to home index after linking discord, try message box
1 parent adbe78d commit 81fa225

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Zero-K.info/Controllers/HomeController.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Newtonsoft.Json.Linq;
2525
using System.Threading;
2626
using System.Threading.Tasks;
27+
using Licho.Utils.Web;
2728

2829
namespace ZeroKWeb.Controllers
2930
{
@@ -240,8 +241,15 @@ public ActionResult NotLoggedIn()
240241

241242
public async Task<ActionResult> DiscordAuth(string code, string state)
242243
{
243-
await Global.Server.DiscordWebApi.LinkAccount(state, code);
244-
return Content("Linking discord account");
244+
if (await Global.Server.DiscordWebApi.LinkAccount(state, code))
245+
{
246+
MessageBox.Show("Discord account linked!");
247+
}
248+
else
249+
{
250+
MessageBox.Show("An error occured while linking the Discord account.");
251+
}
252+
return View("HomeIndex");
245253
}
246254

247255
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]

ZkLobbyServer/DiscordWebApi.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ public string GetAnonymousId(int accountId)
4444
return state;
4545
}
4646

47-
public async Task LinkAccount(string state, string code)
47+
public async Task<bool> LinkAccount(string state, string code)
4848
{
4949
try
5050
{
5151
int accountId;
52-
Trace.TraceInformation("Linking discord id...");
5352
if (!userIds.TryGetValue(state, out accountId))
5453
{
5554
Trace.TraceWarning("Invalid state " + state);
@@ -69,15 +68,12 @@ public async Task LinkAccount(string state, string code)
6968
var response = await new HttpClient().SendAsync(request);
7069
response.EnsureSuccessStatusCode();
7170

72-
Trace.TraceInformation("Sent discord identify request...");
7371
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
7472
var token = payload.Value<string>("access_token");
7573

7674
var discord = new DiscordRestClient();
77-
Trace.TraceInformation("Logging in discord client...");
7875
await discord.LoginAsync(TokenType.Bearer, token);
7976
var discordId = discord.CurrentUser.Id;
80-
Trace.TraceInformation("Got discord id " + discordId);
8177
using (var db = new ZkDataContext())
8278
{
8379
var existing = db.Accounts.FirstOrDefault(x => x.DiscordID == discordId);
@@ -87,15 +83,17 @@ public async Task LinkAccount(string state, string code)
8783
existing.DiscordID = (decimal?)null;
8884
db.SaveChanges();
8985
}
90-
Trace.TraceInformation("Linking discord id " + discordId + " to Account " + existing.Name);
86+
Trace.TraceInformation("Linking discord id " + discordId + " to Account " + accountId);
9187
db.Accounts.FirstOrDefault(x => x.AccountID == accountId).DiscordID = discordId;
9288
db.SaveChanges();
9389
}
9490
}
9591
catch (Exception ex)
9692
{
9793
Trace.TraceError("Error linking discord ID " + ex);
94+
return false;
9895
}
96+
return true;
9997
}
10098
}
10199

0 commit comments

Comments
 (0)