|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Coder.Desktop.App.Services; |
| 5 | +using Coder.Desktop.App.Views; |
| 6 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 7 | +using CommunityToolkit.Mvvm.Input; |
| 8 | +using Microsoft.UI.Xaml; |
| 9 | + |
| 10 | +namespace Coder.Desktop.App.ViewModels; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// The View Model backing the sign in window and all its associated pages. |
| 14 | +/// </summary> |
| 15 | +public partial class SignInViewModel : ObservableObject |
| 16 | +{ |
| 17 | + private readonly ICredentialManager _credentialManager; |
| 18 | + |
| 19 | + [ObservableProperty] |
| 20 | + [NotifyPropertyChangedFor(nameof(CoderUrlError))] |
| 21 | + [NotifyPropertyChangedFor(nameof(GenTokenUrl))] |
| 22 | + public partial string CoderUrl { get; set; } = string.Empty; |
| 23 | + |
| 24 | + [ObservableProperty] |
| 25 | + [NotifyPropertyChangedFor(nameof(CoderUrlError))] |
| 26 | + public partial bool CoderUrlTouched { get; set; } = false; |
| 27 | + |
| 28 | + [ObservableProperty] |
| 29 | + [NotifyPropertyChangedFor(nameof(ApiTokenError))] |
| 30 | + public partial string ApiToken { get; set; } = string.Empty; |
| 31 | + |
| 32 | + [ObservableProperty] |
| 33 | + [NotifyPropertyChangedFor(nameof(ApiTokenError))] |
| 34 | + public partial bool ApiTokenTouched { get; set; } = false; |
| 35 | + |
| 36 | + [ObservableProperty] |
| 37 | + public partial string? SignInError { get; set; } = null; |
| 38 | + |
| 39 | + [ObservableProperty] |
| 40 | + public partial bool SignInLoading { get; set; } = false; |
| 41 | + |
| 42 | + public string? CoderUrlError => CoderUrlTouched ? _coderUrlError : null; |
| 43 | + |
| 44 | + private string? _coderUrlError |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + if (!Uri.TryCreate(CoderUrl, UriKind.Absolute, out var uri)) |
| 49 | + return "Invalid URL"; |
| 50 | + if (uri.Scheme is not "http" and not "https") |
| 51 | + return "Must be a HTTP or HTTPS URL"; |
| 52 | + if (uri.PathAndQuery != "/") |
| 53 | + return "Must be a root URL with no path or query"; |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public string? ApiTokenError => ApiTokenTouched ? _apiTokenError : null; |
| 59 | + |
| 60 | + private string? _apiTokenError => string.IsNullOrWhiteSpace(ApiToken) ? "Invalid token" : null; |
| 61 | + |
| 62 | + public Uri GenTokenUrl |
| 63 | + { |
| 64 | + get |
| 65 | + { |
| 66 | + // In case somehow the URL is invalid, just default to coder.com. |
| 67 | + // The HyperlinkButton will crash the entire app if the URL is |
| 68 | + // invalid. |
| 69 | + try |
| 70 | + { |
| 71 | + var baseUri = new Uri(CoderUrl.Trim()); |
| 72 | + var cliAuthUri = new Uri(baseUri, "/cli-auth"); |
| 73 | + return cliAuthUri; |
| 74 | + } |
| 75 | + catch |
| 76 | + { |
| 77 | + return new Uri("https://coder.com"); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public SignInViewModel(ICredentialManager credentialManager) |
| 83 | + { |
| 84 | + _credentialManager = credentialManager; |
| 85 | + } |
| 86 | + |
| 87 | + public void CoderUrl_FocusLost(object sender, RoutedEventArgs e) |
| 88 | + { |
| 89 | + CoderUrlTouched = true; |
| 90 | + } |
| 91 | + |
| 92 | + public void ApiToken_FocusLost(object sender, RoutedEventArgs e) |
| 93 | + { |
| 94 | + ApiTokenTouched = true; |
| 95 | + } |
| 96 | + |
| 97 | + [RelayCommand] |
| 98 | + public void UrlPage_Next(SignInWindow signInWindow) |
| 99 | + { |
| 100 | + CoderUrlTouched = true; |
| 101 | + if (_coderUrlError != null) return; |
| 102 | + signInWindow.NavigateToTokenPage(); |
| 103 | + } |
| 104 | + |
| 105 | + [RelayCommand] |
| 106 | + public void TokenPage_Back(SignInWindow signInWindow) |
| 107 | + { |
| 108 | + ApiToken = ""; |
| 109 | + signInWindow.NavigateToUrlPage(); |
| 110 | + } |
| 111 | + |
| 112 | + [RelayCommand] |
| 113 | + public async Task TokenPage_SignIn(SignInWindow signInWindow) |
| 114 | + { |
| 115 | + CoderUrlTouched = true; |
| 116 | + ApiTokenTouched = true; |
| 117 | + if (_coderUrlError != null || _apiTokenError != null) return; |
| 118 | + |
| 119 | + try |
| 120 | + { |
| 121 | + SignInLoading = true; |
| 122 | + SignInError = null; |
| 123 | + |
| 124 | + var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15)); |
| 125 | + await _credentialManager.SetCredentials(CoderUrl.Trim(), ApiToken.Trim(), cts.Token); |
| 126 | + |
| 127 | + signInWindow.Close(); |
| 128 | + } |
| 129 | + catch (Exception e) |
| 130 | + { |
| 131 | + SignInError = $"Failed to sign in: {e}"; |
| 132 | + } |
| 133 | + finally |
| 134 | + { |
| 135 | + SignInLoading = false; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments