diff --git a/src/client/Launcher/Controllers/MainController.cs b/src/client/Launcher/Controllers/MainController.cs index 0222fa4e..02c278de 100644 --- a/src/client/Launcher/Controllers/MainController.cs +++ b/src/client/Launcher/Controllers/MainController.cs @@ -1,5 +1,7 @@ using System.Windows.Input; +using Arise.Client.Gateway; using Arise.Client.Launcher.Media; +using Avalonia.Data; namespace Arise.Client.Launcher.Controllers; @@ -11,6 +13,7 @@ public sealed class MainController : LauncherController private string _password = string.Empty; private bool _rememberMe; private bool _isModalVisible; + private string _serverAddress = string.Empty; public bool IsLoggedIn { @@ -48,6 +51,24 @@ public bool IsModalVisible set => this.RaiseAndSetIfChanged(ref _isModalVisible, value); } + public string ServerAddress + { + get => _serverAddress; + set + { + try + { + var svc = Services.GetService()!; + svc.BaseAddress = new Uri(value); + _ = this.RaiseAndSetIfChanged(ref _serverAddress, value); + } + catch (Exception ex) + { + throw new DataValidationException($"{ex.Message}"); + } + } + } + public ICommand LoginCommand { get; } public ICommand RecoverPasswordCommand { get; } @@ -68,6 +89,9 @@ public MainController(IServiceProvider services, MusicPlayer musicPlayer) disposable.Add(Disposable.Create(musicPlayer, static player => player.Stop())); }); + // todo: figure out where to get this URI from at startup + _serverAddress = Services.GetService()!.BaseAddress?.ToString() ?? string.Empty; + LoginCommand = ReactiveCommand.Create(LoginAsync); RecoverPasswordCommand = ReactiveCommand.Create(RecoverPassword); RegisterCommand = ReactiveCommand.Create(Register); @@ -106,14 +130,43 @@ private void RecoverPassword() private async Task LoginAsync() { + // todo: add an IsLoggingIn state to signal the user that the request is being processed + // maybe bind it to a spinning indicator + + // todo: use RememberMe setting (save it locally?) + if (!IsLoggedIn) { - // todo: call GatewayClient - - IsLoggedIn = true; - CurrentAccountName = "Foglio"; + var client = Services.GetService() + ?? throw new InvalidOperationException("GatewayClient service not found"); // todo: idk how we should handle this case, if even possible + + // todo: catch something? + + try + { + var resp = await client.Rest.AuthenticateAccountAsync(Username, Password).ConfigureAwait(true); + + if (resp.IsSuccessStatusCode) + { + IsLoggedIn = true; + CurrentAccountName = Username; + + // todo: handle all the data in the response (ban, mail verification, etc) + } + else + { + // todo: handle login fail + } + } + finally + { + // clear the password as soon as it's been sent + Password = string.Empty; + } + } + else + { + // todo: warn? } - - await Task.CompletedTask.ConfigureAwait(true); } } diff --git a/src/client/Launcher/Views/LoginForm.axaml b/src/client/Launcher/Views/LoginForm.axaml new file mode 100644 index 00000000..3b045ef1 --- /dev/null +++ b/src/client/Launcher/Views/LoginForm.axaml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + - - -