diff --git a/Zero-K.info/Controllers/HomeController.cs b/Zero-K.info/Controllers/HomeController.cs index 1f6e777ba0..2b796e3d1b 100644 --- a/Zero-K.info/Controllers/HomeController.cs +++ b/Zero-K.info/Controllers/HomeController.cs @@ -24,6 +24,7 @@ using Newtonsoft.Json.Linq; using System.Threading; using System.Threading.Tasks; +using Licho.Utils.Web; namespace ZeroKWeb.Controllers { @@ -240,8 +241,15 @@ public ActionResult NotLoggedIn() public async Task DiscordAuth(string code, string state) { - await Global.Server.DiscordWebApi.LinkAccount(state, code); - return Content("Linking discord account"); + if (await Global.Server.DiscordWebApi.LinkAccount(state, code)) + { + MessageBox.Show("Discord account linked!"); + } + else + { + MessageBox.Show("An error occured while linking the Discord account."); + } + return View("HomeIndex"); } [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] diff --git a/ZkLobbyServer/DiscordWebApi.cs b/ZkLobbyServer/DiscordWebApi.cs index a5cc4dfa03..112df2e645 100644 --- a/ZkLobbyServer/DiscordWebApi.cs +++ b/ZkLobbyServer/DiscordWebApi.cs @@ -44,12 +44,11 @@ public string GetAnonymousId(int accountId) return state; } - public async Task LinkAccount(string state, string code) + public async Task LinkAccount(string state, string code) { try { int accountId; - Trace.TraceInformation("Linking discord id..."); if (!userIds.TryGetValue(state, out accountId)) { Trace.TraceWarning("Invalid state " + state); @@ -69,15 +68,12 @@ public async Task LinkAccount(string state, string code) var response = await new HttpClient().SendAsync(request); response.EnsureSuccessStatusCode(); - Trace.TraceInformation("Sent discord identify request..."); var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); var token = payload.Value("access_token"); var discord = new DiscordRestClient(); - Trace.TraceInformation("Logging in discord client..."); await discord.LoginAsync(TokenType.Bearer, token); var discordId = discord.CurrentUser.Id; - Trace.TraceInformation("Got discord id " + discordId); using (var db = new ZkDataContext()) { var existing = db.Accounts.FirstOrDefault(x => x.DiscordID == discordId); @@ -87,7 +83,7 @@ public async Task LinkAccount(string state, string code) existing.DiscordID = (decimal?)null; db.SaveChanges(); } - Trace.TraceInformation("Linking discord id " + discordId + " to Account " + existing.Name); + Trace.TraceInformation("Linking discord id " + discordId + " to Account " + accountId); db.Accounts.FirstOrDefault(x => x.AccountID == accountId).DiscordID = discordId; db.SaveChanges(); } @@ -95,7 +91,9 @@ public async Task LinkAccount(string state, string code) catch (Exception ex) { Trace.TraceError("Error linking discord ID " + ex); + return false; } + return true; } }