Browse Source

Username and timeout

Krzysztof Krysiński 4 months ago
parent
commit
041ad733c1

+ 1 - 0
src/PixiEditor.PixiAuth/PixiAuthClient.cs

@@ -16,6 +16,7 @@ public class PixiAuthClient
     {
         httpClient = new HttpClient();
         httpClient.BaseAddress = new Uri(baseUrl);
+        httpClient.Timeout = TimeSpan.FromSeconds(30);
     }
 
     public async Task<Guid?> GenerateSession(string email)

+ 4 - 2
src/PixiEditor/Data/Localization/Languages/en.json

@@ -1036,12 +1036,14 @@
   "FAIL_LOAD_USER_DATA": "Failed to load saved user data",
   "LOGOUT": "Logout",
   "LOGGED_IN_AS": "Hello",
-  "AVATAR_INFO": "Profile information taken from Gravatar, click to go to gravatar.com",
+  "AVATAR_INFO": "Profile taken from Gravatar.\nEdit at gravatar.com",
   "EMAIL_SENT": "Email sent! Check your inbox.",
   "RESEND_ACTIVATION": "Resend",
   "INVALID_TOKEN": "Session is invalid or expired. Please log in again.",
   "ENTER_EMAIL": "Enter your email",
   "LOGIN_LINK": "Send Login Link",
   "LOGIN_LINK_INFO": "We'll email you a secure link to log in. No password needed.",
-  "LOGIN_WINDOW_TITLE": "Founder's Account"
+  "LOGIN_WINDOW_TITLE": "Founder's Account",
+  "CONNECTION_TIMEOUT": "Connection timed out. Please try again.",
+  "OPEN_LOGIN_WINDOW": "Open login window"
 }

+ 50 - 7
src/PixiEditor/ViewModels/SubViewModels/UserViewModel.cs

@@ -56,6 +56,8 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         User?.EmailHash != null ? $"https://www.gravatar.com/avatar/{User.EmailHash}?s=100&d=initials" : null;
 
     private string currentEmail = string.Empty;
+    private string username;
+
     public string CurrentEmail
     {
         get => currentEmail;
@@ -68,6 +70,15 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         }
     }
 
+    public string Username
+    {
+        get => username;
+        set
+        {
+            SetProperty(ref username, value);
+        }
+    }
+
     public UserViewModel(ViewModelMain owner) : base(owner)
     {
         RequestLoginCommand = new AsyncRelayCommand<string>(RequestLogin, CanRequestLogin);
@@ -111,10 +122,12 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         try
         {
             Guid? session = await PixiAuthClient.GenerateSession(email);
+            string hash = GetEmailHash(email);
             if (session != null)
             {
                 LastError = null;
-                User = new User { SessionId = session.Value, EmailHash = GetEmailHash(email) };
+                Username = null;
+                User = new User { SessionId = session.Value, EmailHash = hash, Username = GenerateUsername(hash) };
                 lastSentHash = User.EmailHash;
                 NotifyProperties();
                 SaveUserInfo();
@@ -128,6 +141,10 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         {
             LastError = new LocalizedString("CONNECTION_ERROR");
         }
+        catch (TaskCanceledException timeoutException)
+        {
+            LastError = new LocalizedString("CONNECTION_TIMEOUT");
+        }
     }
 
     public bool CanRequestLogin(string email)
@@ -174,6 +191,10 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         {
             LastError = new LocalizedString("CONNECTION_ERROR");
         }
+        catch (TaskCanceledException timeoutException)
+        {
+            LastError = new LocalizedString("CONNECTION_TIMEOUT");
+        }
     }
 
     private void RunTimeoutTimers(double timeLeft)
@@ -242,6 +263,10 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         {
             LastError = new LocalizedString("CONNECTION_ERROR");
         }
+        catch (TaskCanceledException timeoutException)
+        {
+            LastError = new LocalizedString("CONNECTION_TIMEOUT");
+        }
 
         return false;
     }
@@ -264,14 +289,20 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
                 LastError = null;
                 User.SessionToken = token;
                 User.SessionExpirationDate = expirationDate;
-                User.Username = GenerateUsername(User.EmailHash);
-                try
+                Task.Run(async () =>
                 {
+                    string username = User.Username;
                     User.Username = await TryFetchUserName(User.EmailHash);
-                }
-                catch
-                {
-                }
+                    Username = User.Username;
+                    if (username != User.Username)
+                    {
+                        Dispatcher.UIThread.Invoke(() =>
+                        {
+                            NotifyProperties();
+                            SaveUserInfo();
+                        });
+                    }
+                });
 
                 CurrentEmail = null;
                 NotifyProperties();
@@ -294,6 +325,10 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         {
             LastError = new LocalizedString("CONNECTION_ERROR");
         }
+        catch (TaskCanceledException timeoutException)
+        {
+            LastError = new LocalizedString("CONNECTION_TIMEOUT");
+        }
 
         return false;
     }
@@ -323,6 +358,9 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         catch (HttpRequestException httpRequestException)
         {
         }
+        catch (TaskCanceledException timeoutException)
+        {
+        }
     }
 
     public async Task SaveUserInfo()
@@ -345,6 +383,7 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
             try
             {
                 User.Username = await TryFetchUserName(User.EmailHash);
+                Username = User.Username;
                 NotifyProperties();
             }
             catch
@@ -387,6 +426,10 @@ internal class UserViewModel : SubViewModel<ViewModelMain>
         {
             LastError = new LocalizedString("CONNECTION_ERROR");
         }
+        catch (TaskCanceledException timeoutException)
+        {
+            LastError = new LocalizedString("CONNECTION_TIMEOUT");
+        }
 
         return GenerateUsername(emailHash);
     }

+ 2 - 1
src/PixiEditor/Views/Auth/LoginForm.axaml

@@ -18,6 +18,7 @@
             <TextBox Text="{Binding CurrentEmail, Mode=TwoWay}" Watermark="{ui:Translate Key=ENTER_EMAIL}" Name="Email"
                      IsVisible="{Binding !IsLoggedIn}" />
             <Button Name="LoginButton" ui:Translator.Key="LOGIN_LINK"
+                    IsDefault="True"
                     Command="{Binding RequestLoginCommand}"
                     CommandParameter="{Binding ElementName=Email, Path=Text}">
                 <Button.IsVisible>
@@ -78,7 +79,7 @@
             <TextBlock HorizontalAlignment="Center" FontSize="{DynamicResource FontSizeNormal}"
                        ui:Translator.Key="LOGGED_IN_AS">
                 <Run Text="" />
-                <Run Text="{Binding User.Username}" />
+                <Run Text="{Binding Username}" />
             </TextBlock>
             <Button
                 Content="{ui:Translate Key=LOGOUT}"