Skip to content

Commit

Permalink
Merge pull request #63 from Etuldan/feature/webpage-twitch-login
Browse files Browse the repository at this point in the history
fix: add webpage after twitch redirect
  • Loading branch information
Etuldan authored May 13, 2023
2 parents b2a9cc0 + 6e70418 commit 0cd55db
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ jobs:
sed -i "s/TWITCH_CLIENTID/${{secrets.TWITCH_CLIENTID}}/g" "MidiControl/App.config"
shell: bash

- name: Replace Version
id: code_version
run: |
sed -i "s/VERSION/${{ steps.vars.outputs.version }}/g" "MidiControl/Properties/AssemblyInfo.cs"
shell: bash

- name: Resource generation
run: |
Set-Location -Path "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\"
Expand Down
6 changes: 6 additions & 0 deletions MidiControl/MidiControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,10 @@
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Update="Resources\redirect.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions MidiControl/Resources/redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MIDIControl - Twitch login - Redirect</title>
</head>
<body>
<div>
<p>MIDIControl - Twitch</p>
<p>Twitch login successful, you can close this page.</p>
</div>
<script>
setTimeout(function() {
window.close()
}, 5000);
</script>
</body>
</html>
36 changes: 20 additions & 16 deletions MidiControl/UI/OptionsGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

Expand Down Expand Up @@ -102,32 +102,36 @@ private void BtnSave_Click(object sender, EventArgs e)
this.Dispose();
}

private void BtnRequestTwitchLogin_Click(object sender, EventArgs e)
private async void BtnRequestTwitchLogin_Click(object sender, EventArgs e)
{
var thread = new Thread(() =>
{
var taskServer = Task.Run(() => {
var server = new WebServer();
options.options.TwitchLogin = server.Login;
options.options.TwitchToken = server.OAuthCode;
options.options.TwitchRefreshToken = server.RefreshToken;

if (options.options.TwitchLogin != "" && options.options.TwitchToken != "")
{
txtBoxTwitchLogin.Text = options.options.TwitchLogin;
TwitchChatControl.GetInstance().Connect();
return options.options.TwitchLogin;
}
return string.Empty;
});

var taskBrowser = Task.Run(() => {
var uriBuilder = new UriBuilder("https://id.twitch.tv/oauth2/authorize?");
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query.Add("response_type", "code");
query.Add("client_id", ConfigurationManager.AppSettings["TwitchClientId"]);
query.Add("redirect_uri", ConfigurationManager.AppSettings["TwitchLocalUrl"]);
query.Add("scope", ConfigurationManager.AppSettings["TwitchScope"]);
query.Add("state", "c3ab8aa609ea11e793ae92361f002671");
uriBuilder.Query = query.ToString();
Process.Start(uriBuilder.Uri.ToString());
});
thread.Start();

var uriBuilder = new UriBuilder("https://id.twitch.tv/oauth2/authorize?");
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query.Add("response_type", "code");
query.Add("client_id", ConfigurationManager.AppSettings["TwitchClientId"]);
query.Add("redirect_uri", ConfigurationManager.AppSettings["TwitchLocalUrl"]);
query.Add("scope", ConfigurationManager.AppSettings["TwitchScope"]);
query.Add("state", "c3ab8aa609ea11e793ae92361f002671");
uriBuilder.Query = query.ToString();
Process.Start(uriBuilder.Uri.ToString());

await Task.WhenAll(taskServer, taskBrowser);
txtBoxTwitchLogin.Text = await taskServer;
}

private void BtnRequestTwitchLogout_Click(object sender, EventArgs e)
Expand Down
5 changes: 4 additions & 1 deletion MidiControl/UI/TwitchLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static MidiControl.OptionsManagment;
Expand Down Expand Up @@ -101,7 +103,8 @@ public async Task HandleIncomingConnections()
catch (ArgumentOutOfRangeException)
{
}
var data = Encoding.UTF8.GetBytes("<html></html>");
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Resources\redirect.html");
var data = File.ReadAllBytes(path);
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
Expand Down
1 change: 1 addition & 0 deletions setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ VersionInfoVersion={#AppVersion}
Source: "{#Dir}MidiControl.exe"; DestDir: "{app}";
Source: "{#Dir}*.dll"; DestDir: "{app}";
Source: "{#Dir}MidiControl.exe.config"; DestDir: "{app}";
Source: "{#Dir}Resources\redirect.html"; DestDir: "{app}\Resources";
Source: "3rd-party-licenses.txt"; DestDir: "{app}";
Source: "LICENSE"; DestDir: "{app}";
Source: "MIDIControl.VisualElementsManifest.xml"; DestDir: "{app}";
Expand Down

0 comments on commit 0cd55db

Please sign in to comment.