login flow

This commit is contained in:
Michele Scandura 2020-11-10 12:14:21 +00:00
parent 886d2a88b0
commit 194f3cbffa
22 changed files with 642 additions and 141 deletions

View file

@ -13,6 +13,10 @@ using System.IO;
using System.Linq;
using System;
using Sledgemapper.Shared.Entities;
using Sledgemapper.UI;
using System.Net;
using System.Net.Http;
using Refit;
namespace Sledgemapper
{
@ -56,6 +60,7 @@ namespace Sledgemapper
var menuFileLoad = new MenuItem("_file_load", "Load");
var menuFileSave = new MenuItem("_file_save", "Save");
var menuConnect = new MenuItem("_connect", "Connect");
var menuConnectLogin = new MenuItem("_connect_login", "Login");
var menuConnectNew = new MenuItem("_connect_new", "New");
var menuConnectJoin = new MenuItem("_connect_join", "Join");
var menuConnectSync = new MenuItem("_connect_sync", "Sync");
@ -63,9 +68,11 @@ namespace Sledgemapper
menuConnectSync.Selected += OnMenuConnectSyncSelected;
menuFileLoad.Selected += OnMenuFileLoadSelected;
menuFileSave.Selected += OnMenuFileSaveSelected;
menuConnectLogin.Selected += OnMenuConnectLoginSelected;
menuConnectNew.Selected += OnMenuConnectNewSelected;
menuConnectJoin.Selected += OnMenuConnectJoinSelected;
menuConnect.Items.Add(menuConnectLogin);
menuConnect.Items.Add(menuConnectNew);
menuConnect.Items.Add(menuConnectJoin);
menuConnect.Items.Add(menuConnectSync);
@ -84,121 +91,270 @@ namespace Sledgemapper
{
Title = "Join mapping session"
};
var content = new VerticalStackPanel();
var grid = new Grid
{
Width = 200,
ShowGridLines = false,
ColumnSpacing = 8,
RowSpacing = 3,
};
// Set partitioning configuration
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.ColumnsProportions.Add(new Proportion(ProportionType.Fill));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
var sessionNameLabel = new Label { Text = "Session Name:" };
var initialsLabel = new Label { Text = "Initials:", GridRow = 1 };
var textbox = new TextBox { GridColumn = 1 };
var initialsTextbox = new TextBox { GridColumn = 1, GridRow = 1 };
TextButton button = new TextButton
{
Text = "Start",
HorizontalAlignment = HorizontalAlignment.Center
};
grid.Widgets.Add(textbox);
grid.Widgets.Add(initialsTextbox);
grid.Widgets.Add(sessionNameLabel);
grid.Widgets.Add(initialsLabel);
content.Widgets.Add(grid);
button.Click += async (s, e) =>
{
if (string.IsNullOrWhiteSpace(textbox.Text))
{
return;
}
if (_communicationManager.Connection.State != HubConnectionState.Connected)
{ await _communicationManager.Connection.StartAsync(); }
var successful = false;
try
{
var result = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", textbox.Text, initialsTextbox.Text);
if (result != null)
{
_sessionData.Map = result.Map;
_sessionData.Walls = result.Walls;
_sessionData.Overlays = result.Overlays;
_sessionData.Players = result.Players;
_sessionData.MapEntityAdded += OnMapEntityAdded;
}
successful = result != null; ;
}
catch { }
if (successful)
{
_sessionData.SessionName = textbox.Text;
window.Close();
}
};
content.Widgets.Add(button);
var content = new SessionWindow();
content.BtnLogin.Text = "Join";
content.BtnLogin.Click += OnButtonJoinSessionClicked;
window.Content = content;
window.ShowModal(_desktop);
}
private async void OnButtonJoinSessionClicked(object sender, EventArgs e)
{
Container container = ((TextButton)sender).Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as SessionWindow;
var isValid = ValidateTextbox(localContent.TxtSession);
if (!isValid)
{
return;
}
if (_communicationManager.Connection.State != HubConnectionState.Connected)
{
await _communicationManager.Connection.StartAsync();
}
var successful = false;
try
{
var result = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text, _authResponse.Initials);
if (result != null)
{
_sessionData.Map = result.Map;
_sessionData.Walls = result.Walls;
_sessionData.Overlays = result.Overlays;
_sessionData.Players = result.Players;
_sessionData.MapEntityAdded += OnMapEntityAdded;
_sessionData.MapEntityDeleted += OnMapEntityDeleted;
}
successful = result != null; ;
}
catch { }
if (successful)
{
_sessionData.SessionName = localContent.TxtSession.Text;
localWindow.Close();
}
}
private AuthenticateResponse _authResponse;
private void OnMenuConnectLoginSelected(object sender, EventArgs e)
{
Window window = new Window
{
Title = "Login"
};
var content = new LoginRegisterWindow();
content.RdoLogin.IsPressed = true;
content.RdoLogin.Click += (s, e) =>
{
content.TxtFirstname.Visible = false;
content.TxtLastname.Visible = false;
content.TxtInitials.Visible = false;
content.LblFirstname.Visible = false;
content.LblLastname.Visible = false;
content.LblInitials.Visible = false;
content.BtnLogin.Visible = true;
content.BtnRegister.Visible = false;
window.Title = "Login";
};
content.RdoRegister.Click += (s, e) =>
{
content.TxtFirstname.Visible = true;
content.TxtLastname.Visible = true;
content.TxtInitials.Visible = true;
content.LblFirstname.Visible = true;
content.LblLastname.Visible = true;
content.LblInitials.Visible = true;
content.BtnLogin.Visible = false;
content.BtnRegister.Visible = true;
window.Title = "Register";
};
content.BtnRegister.Click += OnButtonRegisterClick;
content.BtnLogin.Click += OnButtonLoginClick;
window.Content = content;
window.ShowModal(_desktop);
}
private async void OnButtonLoginClick(object sender, EventArgs e)
{
Container container = ((TextButton)sender).Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as LoginRegisterWindow;
var isValid = true;
isValid &= ValidateTextbox(localContent.TxtEmail);
isValid &= ValidateTextbox(localContent.TxtPassword);
if (!isValid)
{
return;
}
var successful = false;
try
{
var httpClientHandler = new HttpClientHandler();
//if (myConfigurationService.VerifySslCertificate == false)
//{
httpClientHandler.ServerCertificateCustomValidationCallback =
(message, certificate, chain, sslPolicyErrors) => true;
var identiyApi = RestService.For<IIdentityApi>(
new HttpClient(httpClientHandler)
{
BaseAddress = new Uri("http://localhost:4000")
});
_authResponse = await identiyApi.Authenticate(new AuthenticateModel
{
Username = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text
});
successful = true;
}
catch (Exception ex)
{
}
if (successful)
{
localWindow.Close();
};
}
private bool ValidateTextbox(TextBox textBox)
{
var valid = !string.IsNullOrWhiteSpace(textBox.Text);
if (!valid)
{
textBox.Background = new SolidBrush(Color.Red);
}
return valid;
}
private async void OnButtonRegisterClick(object sender, EventArgs e)
{
Container container = ((TextButton)sender).Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as LoginRegisterWindow;
var isValid = true;
isValid &= ValidateTextbox(localContent.TxtEmail);
isValid &= ValidateTextbox(localContent.TxtPassword);
isValid &= ValidateTextbox(localContent.TxtFirstname);
isValid &= ValidateTextbox(localContent.TxtLastname);
isValid &= ValidateTextbox(localContent.TxtInitials);
if (!isValid)
{
return;
}
var successful = false;
try
{
var httpClientHandler = new HttpClientHandler();
//if (myConfigurationService.VerifySslCertificate == false)
//{
httpClientHandler.ServerCertificateCustomValidationCallback =
(message, certificate, chain, sslPolicyErrors) => true;
var identiyApi = RestService.For<IIdentityApi>(
new HttpClient(httpClientHandler)
{
BaseAddress = new Uri("http://localhost:4000")
});
var result = await identiyApi.Register(new RegisterModel
{
Username = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text,
FirstName = localContent.TxtFirstname.Text,
LastName = localContent.TxtLastname.Text,
Initials = localContent.TxtInitials.Text
});
if (result.IsSuccessStatusCode)
{
_authResponse = await identiyApi.Authenticate(new AuthenticateModel
{
Username = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text
});
successful = true;
}
}
catch (Exception ex)
{
}
if (successful)
{
localWindow.Close();
};
}
private void OnMenuConnectNewSelected(object sender, EventArgs e)
{
Window window = new Window
{
Title = "New mapping session"
};
var content = new VerticalStackPanel();
var grid = new Grid
{
Width = 200,
ShowGridLines = false,
ColumnSpacing = 8,
RowSpacing = 3,
};
// Set partitioning configuration
grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto));
grid.ColumnsProportions.Add(new Proportion(ProportionType.Fill));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
grid.RowsProportions.Add(new Proportion(ProportionType.Auto));
var sessionNameLabel = new Label { Text = "Session Name:" };
var initialsLabel = new Label { Text = "Initials:", GridRow = 1 };
var textbox = new TextBox { GridColumn = 1 };
var initialsTextbox = new TextBox { GridColumn = 1, GridRow = 1 };
TextButton button = new TextButton
{
Text = "Start",
HorizontalAlignment = HorizontalAlignment.Center
};
grid.Widgets.Add(textbox);
grid.Widgets.Add(initialsTextbox);
grid.Widgets.Add(sessionNameLabel);
grid.Widgets.Add(initialsLabel);
var content = new SessionWindow();
content.BtnLogin.Text = "Join";
content.BtnLogin.Click += OnButtonNewSessionClicked;
window.Content = content;
content.Widgets.Add(grid);
window.ShowModal(_desktop);
button.Click += async (s, e) =>
}
private async void OnButtonNewSessionClicked(object sender, EventArgs e)
{
Container container = ((TextButton)sender).Parent;
while (!(container is Window))
{
if (string.IsNullOrWhiteSpace(textbox.Text))
{
return;
}
if (_communicationManager.Connection.State != HubConnectionState.Connected)
{ await _communicationManager.Connection.StartAsync(); }
var successful = false;
try
{
var session = await _communicationManager.Connection?.InvokeAsync<Session>("NewSession", textbox.Text, initialsTextbox.Text);
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as SessionWindow;
var isValid = ValidateTextbox(localContent.TxtSession);
if (!isValid)
{
return;
}
if (_communicationManager.Connection.State != HubConnectionState.Connected)
{
await _communicationManager.Connection.StartAsync();
}
var successful = false;
try
{
var session = await _communicationManager.Connection?.InvokeAsync<Session>("NewSession", localContent.TxtSession.Text, _authResponse.Initials);
if (session != null)
{
_sessionData = session;
@ -208,29 +364,14 @@ namespace Sledgemapper
}
successful = session != null;
}
catch (Exception ex)
{
}
if (successful)
{
_sessionData.SessionName = textbox.Text;
_communicationManager.SessionData = _sessionData;
window.Close();
}
};
content.Widgets.Add(button);
window.Content = content;
window.Closed += (s, a) =>
}
catch { }
if (successful)
{
// Called when window is closed
};
window.ShowModal(_desktop);
_sessionData.SessionName = localContent.TxtSession.Text;
_communicationManager.SessionData = _sessionData;
localWindow.Close();
}
}
private void OnMenuFileSaveSelected(object sender, EventArgs e)