Skip to content

Commit

Permalink
move login form to its own view; implement temporary config of the se…
Browse files Browse the repository at this point in the history
…rver uri; actually implement authenticate request to the gateway when pressing the login button
  • Loading branch information
foglio1024 committed Jan 17, 2024
1 parent d65b61f commit 0fcb8e2
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 97 deletions.
65 changes: 59 additions & 6 deletions src/client/Launcher/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
{
Expand Down Expand Up @@ -48,6 +51,24 @@ public bool IsModalVisible
set => this.RaiseAndSetIfChanged(ref _isModalVisible, value);
}

public string ServerAddress
{
get => _serverAddress;
set
{
try
{
var svc = Services.GetService<GatewayClient>()!;
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; }
Expand All @@ -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<GatewayClient>()!.BaseAddress?.ToString() ?? string.Empty;

LoginCommand = ReactiveCommand.Create(LoginAsync);
RecoverPasswordCommand = ReactiveCommand.Create(RecoverPassword);
RegisterCommand = ReactiveCommand.Create(Register);
Expand Down Expand Up @@ -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<GatewayClient>()
?? 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);
}
}
102 changes: 102 additions & 0 deletions src/client/Launcher/Views/LoginForm.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:controls="using:Arise.Client.Launcher.Controls"
xmlns:controllers="using:Arise.Client.Launcher.Controllers"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="400"
x:DataType="controllers:MainController"
x:Class="Arise.Client.Launcher.Views.LoginForm"
Background="Transparent"
>

<Grid RowDefinitions="*,*,*,*,*,*"
>
<Grid.Styles>
<Style Selector="TextBox">
<Setter Property="Margin" Value="0 0 0 10"/>
<Setter Property="Padding" Value="12"/>
<Setter Property="CornerRadius" Value="8"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemeTextMidBrush}"/>
</Style>
</Grid.Styles>
<TextBlock Margin="0 0 0 20"
TextWrapping="Wrap">
<Run Text="TERA Arise"
FontSize="22"
FontWeight="DemiBold"/>
<LineBreak/>
<LineBreak/>
<Run Text="Use your account name and password to login"
Foreground="{DynamicResource ThemeTextMidBrush}"/>
</TextBlock>
<Button HorizontalAlignment="Right"
VerticalAlignment="Top"
Height="28"
Width="28"
CornerRadius="8"
Margin="0"
Background="Transparent"
Command="{Binding CloseModalCommand}">
<avalonia:MaterialIcon Kind="Close" />
</Button>
<TextBox VerticalAlignment="Center"
Watermark="Username"
x:Name="UsernameTextBox"
Text="{Binding Username, Mode=TwoWay}"
Grid.Row="1"
/>
<TextBox PasswordChar=""
Watermark="Password"
VerticalAlignment="Center"
Grid.Row="2"
Text="{Binding Password, Mode=TwoWay}"
/>
<Button Content="Login"
Grid.Row="3"
CornerRadius="8"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Height="{Binding #UsernameTextBox.Bounds.Height}"
Command="{Binding LoginCommand}"
Background="{DynamicResource AccentBrush}"
/>
<CheckBox Grid.Row="4"
Content="Remember me"
FontSize="10"
Padding="4 0 4 0"
Classes="small"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsChecked="{Binding RememberMe}"
/>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="Recover password"
Grid.Row="4"
Padding="0 1 0 0"
FontSize="10"
Foreground="{DynamicResource AccentBrush}"
Cursor="Hand"
controls:CommandBehavior.Command="{Binding RecoverPasswordCommand}"
/>
<!--looks like we have no inline hyperlinks in avalonia
https://github.com/AvaloniaUI/Avalonia/discussions/8818-->
<WrapPanel Grid.Row="5"
Margin="0 10 0 0"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="You don't have an account? " FontSize="10"/>
<TextBlock Text="Register"
FontSize="10"
Foreground="{DynamicResource AccentBrush}"
Cursor="Hand"
controls:CommandBehavior.Command="{Binding RegisterCommand}"
/>
<TextBlock Text=" now." FontSize="10"/>
</WrapPanel>
</Grid>
</UserControl>
9 changes: 9 additions & 0 deletions src/client/Launcher/Views/LoginForm.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Arise.Client.Launcher.Views;

public partial class LoginForm : UserControl
{
public LoginForm()
{
InitializeComponent();
}
}
105 changes: 14 additions & 91 deletions src/client/Launcher/Windows/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:controllers="using:Arise.Client.Launcher.Controllers"
xmlns:avalonia="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:controls="using:Arise.Client.Launcher.Controls"
xmlns:views="using:Arise.Client.Launcher.Views"
x:Class="Arise.Client.Launcher.Windows.MainWindow"
x:DataType="controllers:MainController"
Width="1280"
Expand Down Expand Up @@ -106,114 +107,36 @@
Stretch="Uniform"
Grid.Row="1" />-->

<!--this should only be visible in dev builds
(also move it to settings later)-->
<TextBox Text="{Binding ServerAddress, Mode=TwoWay}"
Grid.Row="1"
VerticalAlignment="Top"
Watermark="Server address"

/>

<Border x:Name="ModalDarkening"
Background="#5000"
Grid.RowSpan="2"
IsHitTestVisible="{Binding IsModalVisible}"
Classes.visible="{Binding IsModalVisible}"
controls:CommandBehavior.Command="{Binding CloseModalCommand}">
>
<Border.Transitions>
<Transitions>
<DoubleTransition Property="Opacity" Duration="0:0:0.25"/>
<DoubleTransition Property="Opacity" Duration="0:0:0.20"/>
</Transitions>
</Border.Transitions>

<Border x:Name="LoginForm"
<Border x:Name="FormContainer"
Background="{DynamicResource WindowBackground}"
CornerRadius="10"
BoxShadow="0 10 20 0 #5000"
Padding="24"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="300"
>
<Border.Styles>
<Style Selector="TextBox">
<Setter Property="Margin" Value="0 0 0 10"/>
<Setter Property="Padding" Value="12"/>
<Setter Property="CornerRadius" Value="8"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemeTextMidBrush}"/>
</Style>
</Border.Styles>
<Grid RowDefinitions="*,*,*,*,*,*">
<TextBlock Margin="0 0 0 20"
TextWrapping="Wrap">
<Run Text="TERA Arise"
FontSize="22"
FontWeight="DemiBold"/>
<LineBreak/>
<LineBreak/>
<Run Text="Use your account name and password to login"
Foreground="{DynamicResource ThemeTextMidBrush}"/>
</TextBlock>
<Button HorizontalAlignment="Right"
VerticalAlignment="Top"
Height="28"
Width="28"
CornerRadius="14"
Background="Transparent"
Command="{Binding CloseModalCommand}">
<avalonia:MaterialIcon Kind="Close" />
</Button>
<TextBox VerticalAlignment="Center"
Watermark="Username"
x:Name="UsernameTextBox"
Text="{Binding Username, Mode=TwoWay}"
Grid.Row="1"
/>
<TextBox PasswordChar=""
Watermark="Password"
VerticalAlignment="Center"
Grid.Row="2"
Text="{Binding Password, Mode=TwoWay}"
/>
<Button Content="Login"
Grid.Row="3"
CornerRadius="8"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Height="{Binding #UsernameTextBox.Bounds.Height}"
Command="{Binding LoginCommand}"
Background="{DynamicResource AccentBrush}"
/>
<CheckBox Grid.Row="4"
Content="Remember me"
FontSize="10"
Padding="4 0 4 0"
Classes="small"
HorizontalAlignment="Left"
VerticalAlignment="Center"
IsChecked="{Binding RememberMe}"
/>
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="Recover password"
Grid.Row="4"
Padding="0 1 0 0"
FontSize="10"
Foreground="{DynamicResource AccentBrush}"
Cursor="Hand"
controls:CommandBehavior.Command="{Binding RecoverPasswordCommand}"
/>
<!--looks like we have no inline hyperlinks in avalonia
https://github.com/AvaloniaUI/Avalonia/discussions/8818-->
<WrapPanel Grid.Row="5"
Margin="0 10 0 0"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBlock Text="You don't have an account? " FontSize="10"/>
<TextBlock Text="Register"
FontSize="10"
Foreground="{DynamicResource AccentBrush}"
Cursor="Hand"
controls:CommandBehavior.Command="{Binding RegisterCommand}"
/>
<TextBlock Text=" now." FontSize="10"/>
</WrapPanel>
</Grid>
Width="300">
<views:LoginForm/>
</Border>
</Border>
</Grid>
Expand Down

0 comments on commit 0fcb8e2

Please sign in to comment.