Skip to content

Commit

Permalink
login form
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-andrich committed Jul 25, 2023
1 parent 6143866 commit a4c25a7
Show file tree
Hide file tree
Showing 9 changed files with 2,875 additions and 84 deletions.
1,013 changes: 1,013 additions & 0 deletions maERP.Server/Migrations/20230725194616_initDatabase.Designer.cs

Large diffs are not rendered by default.

661 changes: 661 additions & 0 deletions maERP.Server/Migrations/20230725194616_initDatabase.cs

Large diffs are not rendered by default.

1,010 changes: 1,010 additions & 0 deletions maERP.Server/Migrations/ApplicationDbContextModelSnapshot.cs

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions maERP.Shared/Models/LoginFormModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ namespace maERP.Shared.Models;

public class LoginFormModel
{
public string? UserName { get; set; } = string.Empty;
public string? Password { get; set; } = string.Empty;
public List<string>? ServerList { get; set; }
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Server { get; set; } = string.Empty;
public bool RememberMe { get; set; } = false;
}

public class LoginServer
{
public string Url { get; set; } = string.Empty;
public string Version { get; set; } = string.Empty;
public DateTime LastUsed { get; set; }
}

public class LoginFormModelFluentValidator : AbstractValidator<LoginFormModel>
{
private readonly IStringLocalizer<LoginFormModelFluentValidator> _localizer;
Expand Down
126 changes: 47 additions & 79 deletions maERP.Shared/Pages/Auth/Login.razor
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
@page "/login"

@using maERP.Shared.Dtos.User
@using maERP.Shared.Models;

@inject NavigationManager _navManager
@inject Blazored.LocalStorage.ILocalStorageService _localStorage
@inject maERP.Shared.Contracts.IAuthenticationService _authenticationService

<MudText Typo="Typo.h4" Class="mb-8">maERP</MudText>
@*<MudIcon Icon="@Icons.Custom.Brands.MudBlazor" Size="Size.Large" Style="width:100px; height:100px;" />*@
<MudText Typo="Typo.h4" Class="mb-8" Align="Align.Center">maERP</MudText>

<MudForm Model="@_model" @ref="@_form" @bind-IsValid="@_success" ValidationDelay="0">

<MudText Typo="Typo.h4" GutterBottom="true">Login</MudText>
@*
<MudText>
Kein Account? <MudLink Href="/pages/authentication/register">Jetzt registrieren</MudLink>
</MudText>
<MudText>
Kein Account? <MudLink Href="/pages/authentication/register">Jetzt registrieren</MudLink>
</MudText>
*@
<MudTextField T="string"
@bind-Value="_model.UserName" For="@(() => _model.UserName)"
Expand All @@ -24,25 +18,29 @@
Required="true"
OnlyValidateIfDirty="true"
Immediate="true"
Class="my-6">
Class="mt-6">
</MudTextField>

<MudTextField @bind-Value="_model.Password" For="@(() => _model.Password)"
Label="Passwort"
Variant="Variant.Outlined"
Required="true"
InputType="InputType.Password">
</MudTextField>

<MudTextField @bind-Value="_model.ServerList" For="@(() => _model.ServerList)"
Label="Server"
Label="Passwort"
Variant="Variant.Outlined"
Required="true"
InputType="InputType.Password">
InputType="InputType.Password"
Class="mt-6">
</MudTextField>

<MudStack Row="true" Class="mt-6">
<MudSelect Label="Server" @bind-Value="_model.Server" Variant="Variant.Outlined" Required="true">
@foreach (var server in _serverList)
{
<MudSelectItem Value="@server.Url">@server.Url</MudSelectItem>
}
</MudSelect>
<MudIconButton Icon="@Icons.Material.Outlined.Edit" OnClick="OpenServerOverlay" />
</MudStack>

<div Class="d-flex justify-space-between align-center">
<MudCheckBox For="@(() => _model.RememberMe)" @bind-Checked="_model.RememberMe" Label="Login speichern" Color="Color.Primary" Class="ml-n1 my-3"></MudCheckBox>
<MudCheckBox For="@(() => _model.RememberMe)" @bind-Checked="_model.RememberMe" Label="Eingeloggt bleiben" Color="Color.Primary" Class="ml-n1 my-3"></MudCheckBox>
<MudLink Href="/pages/authentication/forgot-password">Passwort vergessen</MudLink>
</div>

Expand All @@ -66,60 +64,30 @@
</MudButton>
</MudForm>


@code
{
private string _title = "Login";

MudForm? _form;
bool _success;
bool _loading;

List<string> _server = new()
{
"https://localhost:8443"
};

LoginFormModel _model = new()
{
UserName = "admin@localhost.com",
Password = "maERP!12",
// Server = _server,
RememberMe = true
};

_model.ServerList = _server;

// LoginDto loginDto = new LoginDto { Email = "admin@localhost.com", Password = "maERP!12", Server = "https://localhost:8443" };
private string _spinnerClass = string.Empty;
private string _errorMessage = string.Empty;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_title = "Login";
}

private async void OnSubmit()
{
_spinnerClass = "spinner-border spinner-border-sm";

await _localStorage.SetItemAsStringAsync("server", _model.Server);

var loginResponse = await _authenticationService.AuthenticateAsync(_model.UserName, _model.Password);

if (loginResponse == true)
{
_navManager.NavigateTo("/");
return;
}

_errorMessage = "Login fehlgeschlagen";
_spinnerClass = "";

await _localStorage.RemoveItemAsync("server");

this.StateHasChanged();
}
}
<MudOverlay @bind-Visible="_showServerOverlay" DarkBackground="true" AutoClose="false">
<MudPaper Width="600px" Height="400px">

<MudContainer>
<MudDataGrid Items="@_serverList" Filterable="false" SortMode="@SortMode.None" Groupable="false">
<Columns>
<PropertyColumn Property="x => x.Url" />
<PropertyColumn Property="x => x.LastUsed" />
<TemplateColumn>
<CellTemplate>
<MudStack Row>
<MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Delete" OnClick="@(() => RemoveFromServerList(@context.Item.Url))" />
</MudStack>
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>

<MudStack Row="true" Class="mt-6">
<MudTextField Label="Server hinzufügen" Variant="Variant.Outlined" @bind-Value="@newServer" />
<MudIconButton Icon="@Icons.Material.Outlined.Add" OnClick="AddToServerList">Add</MudIconButton>
</MudStack>

<MudButton FullWidth="true" Variant="Variant.Filled" Color="Color.Primary" OnClick="OpenServerOverlay" Class="mt-6">Schließen</MudButton>
</MudContainer>
</MudPaper>
</MudOverlay>
130 changes: 130 additions & 0 deletions maERP.Shared/Pages/Auth/Login.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using maERP.Shared.Models;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using maERP.Shared.Contracts;
using Blazored.LocalStorage;
using System.Text.Json;

namespace maERP.Shared.Pages.Auth;

public partial class Login
{
[Inject]
private NavigationManager? _navManager { get; set; }

[Inject]
private ILocalStorageService? _localStorage { get; set; }

[Inject]
private IAuthenticationService? _authenticationService { get; set; }

private string _title = "Login";
private bool _showServerOverlay;
private string newServer = string.Empty;

MudForm? _form;
bool _success;
bool _loading;

List<LoginServer> _serverList = new();
LoginFormModel _model = new();

private string _spinnerClass = string.Empty;
private string _errorMessage = string.Empty;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

if(await _localStorage.ContainKeyAsync("serverList"))
{
try
{
string serverJson = await _localStorage.GetItemAsStringAsync("serverList");
_serverList = JsonSerializer.Deserialize<List<LoginServer>>(serverJson);
}
catch
{
await _localStorage.RemoveItemAsync("serverList");
}
}

SelectFirstServerFromList();

if(await _localStorage.ContainKeyAsync("username"))
{
_model.UserName = await _localStorage.GetItemAsStringAsync("username");
}
}

public void OpenServerOverlay()
{
if (_showServerOverlay)
{
_showServerOverlay = false;
SelectFirstServerFromList();
}
else
{
_showServerOverlay = true;
}

StateHasChanged();
}

void RemoveFromServerList(string s)
{
_serverList = _serverList.Where(u => u.Url != s.ToString()).ToList();
}

void AddToServerList()
{
var newServerItem = new LoginServer
{
Url = newServer,
LastUsed = DateTime.MinValue,
Version = string.Empty
};

_serverList.Add(newServerItem);
newServer = string.Empty;
}

void SelectFirstServerFromList()
{
if (_serverList.Count > 0)
{
_model.Server = _serverList.FirstOrDefault().Url;
}
}

private async void OnSubmit()
{
_spinnerClass = "spinner-border spinner-border-sm";

await _localStorage.SetItemAsStringAsync("server", _model.Server);

var loginResponse = await _authenticationService.AuthenticateAsync(_model.UserName, _model.Password);

if (loginResponse == true)
{
if(_model.RememberMe == true)
{
await _localStorage.SetItemAsStringAsync("username", _model.UserName);
}

string serverJson = JsonSerializer.Serialize(_serverList);
await _localStorage.SetItemAsStringAsync("serverList", serverJson);

_navManager.NavigateTo("/");
return;
}

_errorMessage = "Login fehlgeschlagen";
_spinnerClass = "";

await _localStorage.RemoveItemAsync("server");

this.StateHasChanged();
}
}
1 change: 0 additions & 1 deletion maERP.Shared/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<MudContainer MaxWidth="MaxWidth.Small" Class="d-flex align-center" Style="height: 100vh;">
<div class="d-flex flex-column mud-width-full">
<MudPaper Elevation="25" Class="pa-8" Width="100%" MaxWidth="500px">
<MudIcon Icon="@Icons.Custom.Brands.MudBlazor" Size="Size.Large" Style="width:100px; height:100px;" />
@Body
</MudPaper>
</div>
Expand Down
4 changes: 4 additions & 0 deletions maERP.Shared/maERP.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@
<ItemGroup>
<None Remove="Models\Identity\" />
</ItemGroup>
<ItemGroup>
<Content Remove="Pages\Auth\Login %28Kopie%29.razor" />
<Content Remove="Pages\Auth\Login.razor.cs" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion maERP.Web/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<base href="/" />
<link href="css/app.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png" />
<link href="maERP.Web.styles.css" rel="stylesheet" />
<link href="manifest.json" rel="manifest" />
<link rel="apple-touch-icon" sizes="512x512" href="icon-512.png" />
<link rel="apple-touch-icon" sizes="192x192" href="icon-192.png" />
Expand Down

0 comments on commit a4c25a7

Please sign in to comment.