Skip to content

Commit

Permalink
Redirect to home index after linking discord, try message box
Browse files Browse the repository at this point in the history
  • Loading branch information
DeinFreund committed May 15, 2019
1 parent adbe78d commit 81fa225
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 10 additions & 2 deletions Zero-K.info/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Threading.Tasks;
using Licho.Utils.Web;

namespace ZeroKWeb.Controllers
{
Expand Down Expand Up @@ -240,8 +241,15 @@ public ActionResult NotLoggedIn()

public async Task<ActionResult> 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)]
Expand Down
10 changes: 4 additions & 6 deletions ZkLobbyServer/DiscordWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ public string GetAnonymousId(int accountId)
return state;
}

public async Task LinkAccount(string state, string code)
public async Task<bool> LinkAccount(string state, string code)
{
try
{
int accountId;
Trace.TraceInformation("Linking discord id...");
if (!userIds.TryGetValue(state, out accountId))
{
Trace.TraceWarning("Invalid state " + state);
Expand All @@ -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<string>("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);
Expand All @@ -87,15 +83,17 @@ 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();
}
}
catch (Exception ex)
{
Trace.TraceError("Error linking discord ID " + ex);
return false;
}
return true;
}
}

Expand Down

0 comments on commit 81fa225

Please sign in to comment.