Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a05dc4d

Browse files
Redirecting to the profile page after OAuth login
1 parent 20a5607 commit a05dc4d

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/GitHub.Api/IOAuthCallbackListener.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public interface IOAuthCallbackListener
1616
/// <param name="cancel">A cancellation token.</param>
1717
/// <returns>The temporary code included in the callback.</returns>
1818
Task<string> Listen(string id, CancellationToken cancel);
19+
20+
void RedirectCloseStop(Uri url);
1921
}
2022
}

src/GitHub.Api/LoginManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public async Task<LoginResult> LoginViaOAuth(
157157
await keychain.Save("[oauth]", token.AccessToken, hostAddress).ConfigureAwait(false);
158158
var result = await ReadUserWithRetry(client).ConfigureAwait(false);
159159
await keychain.Save(result.User.Login, token.AccessToken, hostAddress).ConfigureAwait(false);
160+
oauthListener.RedirectCloseStop(hostAddress.WebUri.Append(result.User.Login));
161+
160162
return result;
161163
}
162164

src/GitHub.App/Services/OAuthCallbackListener.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.Composition;
1+
using System;
2+
using System.ComponentModel.Composition;
23
using System.Net;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -39,6 +40,7 @@ public OAuthCallbackListener(IHttpListener httpListener)
3940
}
4041

4142
public readonly static string CallbackUrl = Invariant($"http://localhost:{CallbackPort}/");
43+
private IHttpListenerContext lastContext;
4244

4345
public async Task<string> Listen(string id, CancellationToken cancel)
4446
{
@@ -51,22 +53,29 @@ public async Task<string> Listen(string id, CancellationToken cancel)
5153
{
5254
while (true)
5355
{
54-
var context = await httpListener.GetContextAsync().ConfigureAwait(false);
55-
var foo = context.Request;
56-
var queryParts = HttpUtility.ParseQueryString(context.Request.Url.Query);
56+
lastContext = await httpListener.GetContextAsync().ConfigureAwait(false);
57+
var queryParts = HttpUtility.ParseQueryString(lastContext.Request.Url.Query);
5758

5859
if (queryParts["state"] == id)
5960
{
60-
context.Response.Close();
6161
return queryParts["code"];
6262
}
6363
}
6464
}
6565
}
66-
finally
66+
catch(Exception)
6767
{
6868
httpListener.Stop();
69+
throw;
6970
}
7071
}
72+
73+
public void RedirectCloseStop(Uri url)
74+
{
75+
lastContext.Response.Redirect(url);
76+
lastContext.Response.Close();
77+
78+
httpListener.Stop();
79+
}
7180
}
7281
}

0 commit comments

Comments
 (0)