Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 209de48

Browse files
Merge pull request #400 from github-for-unity/fixes/auth-view-cleanup
Cleanups to #300
2 parents cafb7e7 + 3709d52 commit 209de48

File tree

2 files changed

+36
-69
lines changed

2 files changed

+36
-69
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/AuthenticationView.cs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class AuthenticationView : Subview
99
{
1010
private static readonly Vector2 viewSize = new Vector2(290, 290);
1111

12+
private const string CredentialsNeedRefreshMessage = "We've detected that your stored credentials are out of sync with your current user. This can happen if you have signed in to git outside of Unity. Sign in again to refresh your credentials.";
13+
private const string NeedAuthenticationMessage = "We need you to authenticate first";
1214
private const string WindowTitle = "Authenticate";
1315
private const string UsernameLabel = "Username";
1416
private const string PasswordLabel = "Password";
@@ -24,10 +26,10 @@ class AuthenticationView : Subview
2426
[SerializeField] private string username = string.Empty;
2527
[SerializeField] private string two2fa = string.Empty;
2628
[SerializeField] private string message;
29+
[SerializeField] private string errorMessage;
30+
[SerializeField] private bool need2fa;
2731

28-
[NonSerialized] private bool need2fa;
2932
[NonSerialized] private bool isBusy;
30-
[NonSerialized] private string errorMessage;
3133
[NonSerialized] private bool enterPressed;
3234
[NonSerialized] private string password = string.Empty;
3335
[NonSerialized] private AuthenticationService authenticationService;
@@ -37,18 +39,30 @@ public override void InitializeView(IView parent)
3739
{
3840
base.InitializeView(parent);
3941
need2fa = isBusy = false;
42+
message = errorMessage = null;
4043
Title = WindowTitle;
4144
Size = viewSize;
4245
}
4346

44-
public override void OnEnable()
47+
public void Initialize(Exception exception)
4548
{
46-
base.OnEnable();
47-
}
49+
var usernameMismatchException = exception as TokenUsernameMismatchException;
50+
if (usernameMismatchException != null)
51+
{
52+
message = CredentialsNeedRefreshMessage;
53+
username = usernameMismatchException.CachedUsername;
54+
}
4855

49-
public override void OnDisable()
50-
{
51-
base.OnDisable();
56+
var keychainEmptyException = exception as KeychainEmptyException;
57+
if (keychainEmptyException != null)
58+
{
59+
message = NeedAuthenticationMessage;
60+
}
61+
62+
if (usernameMismatchException == null && keychainEmptyException == null)
63+
{
64+
message = exception.Message;
65+
}
5266
}
5367

5468
public override void OnGUI()
@@ -81,27 +95,7 @@ public override void OnGUI()
8195
}
8296
GUILayout.EndScrollView();
8397
}
84-
85-
public void SetMessage(string value)
86-
{
87-
message = value;
88-
}
89-
90-
public void ClearMessage()
91-
{
92-
message = null;
93-
}
94-
95-
public void SetUsername(string value)
96-
{
97-
username = value;
98-
}
99-
100-
public void ClearUsername()
101-
{
102-
username = string.Empty;
103-
}
104-
98+
10599
private void HandleEnterPressed()
106100
{
107101
if (Event.current.type != EventType.KeyDown)
@@ -174,8 +168,7 @@ private void OnGUI2FA()
174168
if (GUILayout.Button(BackButton))
175169
{
176170
GUI.FocusControl(null);
177-
need2fa = false;
178-
Redraw();
171+
Clear();
179172
}
180173

181174
if (GUILayout.Button(TwofaButton) || (!isBusy && enterPressed))
@@ -196,27 +189,36 @@ private void OnGUI2FA()
196189

197190
private void DoRequire2fa(string msg)
198191
{
199-
Logger.Trace("Strating 2FA - Message:\"{0}\"", msg);
192+
Logger.Trace("Starting 2FA - Message:\"{0}\"", msg);
200193

201194
need2fa = true;
202195
errorMessage = msg;
203196
isBusy = false;
204197
Redraw();
205198
}
206199

200+
private void Clear()
201+
{
202+
need2fa = false;
203+
errorMessage = null;
204+
isBusy = false;
205+
Redraw();
206+
}
207+
207208
private void DoResult(bool success, string msg)
208209
{
209210
Logger.Trace("DoResult - Success:{0} Message:\"{1}\"", success, msg);
210211

211-
errorMessage = msg;
212212
isBusy = false;
213213

214214
if (success == true)
215215
{
216+
Clear();
216217
Finish(true);
217218
}
218219
else
219220
{
221+
errorMessage = msg;
220222
Redraw();
221223
}
222224
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/PopupWindow.cs

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ namespace GitHub.Unity
77
[Serializable]
88
class PopupWindow : BaseWindow
99
{
10-
private const string CredentialsNeedRefreshMessage = "We've detected that your stored credentials are out of sync with your current user. This can happen if you have signed in to git outside of Unity. Sign in again to refresh your credentials.";
11-
private const string NeedAuthenticationMessage = "We need you to authenticate first";
12-
1310
public enum PopupViewType
1411
{
1512
None,
@@ -63,32 +60,8 @@ private void Open(PopupViewType popupViewType, Action<bool> onClose)
6360

6461
}, exception => {
6562
Logger.Trace("User required validation opening AuthenticationView");
66-
67-
string message = null;
68-
string username = null;
69-
70-
var usernameMismatchException = exception as TokenUsernameMismatchException;
71-
if (usernameMismatchException != null)
72-
{
73-
message = CredentialsNeedRefreshMessage;
74-
username = usernameMismatchException.CachedUsername;
75-
}
76-
77-
var keychainEmptyException = exception as KeychainEmptyException;
78-
if (keychainEmptyException != null)
79-
{
80-
message = NeedAuthenticationMessage;
81-
}
82-
83-
if (usernameMismatchException == null && keychainEmptyException == null)
84-
{
85-
message = exception.Message;
86-
}
87-
63+
authenticationView.Initialize(exception);
8864
OpenInternal(PopupViewType.AuthenticationView, completedAuthentication => {
89-
authenticationView.ClearMessage();
90-
authenticationView.ClearUsername();
91-
9265
if (completedAuthentication)
9366
{
9467
Logger.Trace("User completed validation opening view: {0}", popupViewType.ToString());
@@ -98,11 +71,6 @@ private void Open(PopupViewType popupViewType, Action<bool> onClose)
9871
});
9972

10073
shouldCloseOnFinish = false;
101-
authenticationView.SetMessage(message);
102-
if (username != null)
103-
{
104-
authenticationView.SetUsername(username);
105-
}
10674
});
10775
}
10876
else
@@ -120,10 +88,7 @@ private void OpenInternal(PopupViewType popupViewType, Action<bool> onClose)
12088
}
12189

12290
ActiveViewType = popupViewType;
123-
titleContent = new GUIContent(ActiveView.Title, Styles.SmallLogo);
124-
OnEnable();
12591
Show();
126-
Refresh();
12792
}
12893

12994
public IApiClient Client

0 commit comments

Comments
 (0)