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

Commit d95da0a

Browse files
committed
Provide better tooltip when login fails
When the user provides a personal access token instead of their password, the API responds with a 403. We should show a more helpful error message here. Fixes #14
1 parent d33b726 commit d95da0a

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/GitHub.App/ViewModels/LoginTabViewModel.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ protected LoginTabViewModel(IRepositoryHosts repositoryHosts, IVisualStudioBrows
4141

4242
Login.ThrownExceptions.Subscribe(ex =>
4343
{
44-
if (!ex.IsCriticalException())
44+
if (ex.IsCriticalException()) return;
45+
46+
log.Info(string.Format(CultureInfo.InvariantCulture, "Error logging into '{0}' as '{1}'", BaseUri,
47+
UsernameOrEmail), ex);
48+
if (ex is ForbiddenException)
49+
{
50+
ShowLogInFailedError = true;
51+
LoginFailedMessage = "Make sure to use your password and not a Personal Access token to log in.";
52+
}
53+
else
4554
{
46-
log.Info(string.Format(CultureInfo.InvariantCulture, "Error logging into '{0}' as '{1}'", BaseUri, UsernameOrEmail), ex);
4755
ShowConnectingToHostFailed = true;
4856
}
4957
});

src/GitHub.Exports.Reactive/ViewModels/ILoginToHostViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public interface ILoginToHostViewModel
5959
/// </summary>
6060
bool ShowLogInFailedError { get; }
6161

62+
/// <summary>
63+
/// The message to show if login failed.
64+
/// </summary>
65+
string LoginFailedMessage { get; }
66+
6267
/// <summary>
6368
/// Gets a command which, when invoked, resets all properties
6469
/// and validators.

src/UnitTests/GitHub.App/ViewModels/LoginToGitHubViewModelTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
using System;
2+
using System.Net;
3+
using System.Reactive.Linq;
4+
using GitHub.Authentication;
25
using GitHub.Info;
36
using GitHub.Models;
47
using GitHub.Primitives;
58
using GitHub.Services;
69
using GitHub.ViewModels;
710
using NSubstitute;
11+
using Octokit;
812
using Xunit;
913

1014
public class LoginToGitHubViewModelTests
1115
{
16+
public class TheLoginCommand : TestBaseClass
17+
{
18+
[Fact]
19+
public void ShowsHelpfulTooltipWhenForbiddenResponseReceived()
20+
{
21+
var response = Substitute.For<IResponse>();
22+
response.StatusCode.Returns(HttpStatusCode.Forbidden);
23+
var repositoryHosts = Substitute.For<IRepositoryHosts>();
24+
repositoryHosts.LogIn(HostAddress.GitHubDotComHostAddress, Args.String, Args.String)
25+
.Returns(_ => Observable.Throw<AuthenticationResult>(new ForbiddenException(response)));
26+
var browser = Substitute.For<IVisualStudioBrowser>();
27+
var loginViewModel = new LoginToGitHubViewModel(repositoryHosts, browser);
28+
29+
loginViewModel.Login.Execute(null);
30+
31+
Assert.True(loginViewModel.ShowLogInFailedError);
32+
Assert.Equal("Make sure to use your password and not a Personal Access token to log in.",
33+
loginViewModel.LoginFailedMessage);
34+
}
35+
}
36+
1237
public class TheSignupCommand : TestBaseClass
1338
{
1439
[Fact]

0 commit comments

Comments
 (0)