Updated login page, updated submodule

This commit is contained in:
max 2024-11-04 15:46:47 +01:00
parent bd8930a755
commit 9f2b5ef49a
5 changed files with 50 additions and 36 deletions

@ -1 +1 @@
Subproject commit 8531079a16df9bd10c305d22075d7a135f8f8878 Subproject commit 58739c2aeadc7e608359a20cd3ada2d447284c22

View File

@ -12,12 +12,12 @@
@*MudBlazor*@ @*MudBlazor*@
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" /> <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" /> <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)"/> <HeadOutlet @rendermode="BasedAuthDefaults.InteractiveServerWithoutPrerender"/>
</head> </head>
<body> <body>
@*Rendermode to 'InteractiveServer' or else MudBlazor will not work with interactivity*@ @*Rendermode to 'InteractiveServer' or else MudBlazor will not work with interactivity*@
<Routes @rendermode="new InteractiveServerRenderMode(prerender: false)"/> <Routes @rendermode="BasedAuthDefaults.InteractiveServerWithoutPrerender"/>
<script src="_framework/blazor.web.js"></script> <script src="_framework/blazor.web.js"></script>
@*MudBlazor*@ @*MudBlazor*@
<script src="_content/MudBlazor/MudBlazor.min.js"></script> <script src="_content/MudBlazor/MudBlazor.min.js"></script>

View File

@ -1,56 +1,65 @@
@page "/Auth/Login" @page "/Auth/Login"
@using DotBased.ASP.Auth.Domains @using DotBased.ASP.Auth.Domains
@using DotBased.ASP.Auth.Services @using DotBased.ASP.Auth.Services
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@using SharpRSS.Blazor.Extensions @using SharpRSS.Blazor.Extensions
@using SharpRSS.Data.Domains.Auth
@inject ProtectedLocalStorage LocalStorage
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject SecurityService SecurityService @inject SecurityService SecurityService
@inject ISnackbar Snackbar @inject ISnackbar Snackbar
<PageTitle>Login | SharpRSS</PageTitle> <PageTitle>Login | SharpRSS</PageTitle>
<EditForm Model="@model" OnValidSubmit="ValidSubmit"> <MudElement>
<DataAnnotationsValidator/> <MudOverlay DarkBackground="true" Visible="true">
<MudGrid> <EditForm Model="@model" OnValidSubmit="ValidSubmit">
<MudItem> <DataAnnotationsValidator/>
<MudCard> <MudGrid>
<MudCardContent> <MudItem>
<MudTextField Label="UserName" @bind-Value="model.UserName" For="@(() => model.UserName)"/> <MudCard>
<MudTextField Label="Password" @bind-Value="model.Password" For="@(() => model.Password)" InputType="InputType.Password"/> <MudCardHeader>
</MudCardContent> <MudText>SharpRSS login</MudText>
<MudCardActions> </MudCardHeader>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">Login</MudButton> <MudCardContent>
</MudCardActions> <MudTextField Label="UserName" @bind-Value="model.UserName" For="@(() => model.UserName)"/>
</MudCard> <MudTextField Label="Password" @bind-Value="model.Password" For="@(() => model.Password)" InputType="InputType.Password"/>
</MudItem> </MudCardContent>
</MudGrid> <MudCardActions>
</EditForm> <MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary">Login</MudButton>
</MudCardActions>
</MudCard>
</MudItem>
</MudGrid>
</EditForm>
</MudOverlay>
</MudElement>
@code { @code {
[CascadingParameter]
private Task<AuthenticationState> AuthStateTask { get; set; } = null!;
private string RedirectUrl { get; set; } = string.Empty;
readonly LoginModel model = new();
private string RedirectAfterLogin { get; set; } = string.Empty; protected override async Task OnInitializedAsync()
LoginModel model = new();
protected override Task OnInitializedAsync()
{ {
RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : "/"; RedirectUrl = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : NavigationManager.BaseUri;
//TODO: Checking based auth or external (OIDC, etc.) var authState = await AuthStateTask;
return base.OnInitializedAsync(); if (authState.User.Identity is { IsAuthenticated: true })
NavigationManager.NavigateTo(RedirectUrl, true);
} }
private async void ValidSubmit(EditContext obj) private async void ValidSubmit(EditContext obj)
{ {
var loginResult = await SecurityService.LoginAsync(model); try
if (loginResult.Success && loginResult.Value != null)
{ {
await LocalStorage.SetAsync("dotbased_session", loginResult.Value.Id); var loginResult = await SecurityService.LoginAsync(model);
NavigationManager.NavigateTo(RedirectAfterLogin); if (loginResult is { Success: true, Value: not null })
NavigationManager.NavigateTo(RedirectUrl, true);
Snackbar.Add(loginResult.Message, Severity.Error);
}
catch (Exception e)
{
Snackbar.Add(e.Message, Severity.Error);
} }
Snackbar.Add(loginResult.Message, Severity.Error);
} }
} }

View File

@ -8,6 +8,7 @@
@using Microsoft.AspNetCore.Components.Authorization @using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.JSInterop @using Microsoft.JSInterop
@using static Microsoft.AspNetCore.Components.Web.RenderMode @using static Microsoft.AspNetCore.Components.Web.RenderMode
@using DotBased.ASP.Auth
@*SharpRSS*@ @*SharpRSS*@
@using SharpRSS.Blazor @using SharpRSS.Blazor
@using SharpRSS.Blazor.Components @using SharpRSS.Blazor.Components

View File

@ -21,7 +21,11 @@ builder.Services.AddBasedServerAuth(options =>
options.SeedData = service => options.SeedData = service =>
{ {
service.CreateUserAsync(new UserModel() { UserName = "Admin", Name = "Administrator", FamilyName = "admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", service.CreateUserAsync(new UserModel() { UserName = "Admin", Name = "Administrator", FamilyName = "admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password",
Roles = [new RoleModel { Name = "Admin", Description = "Administration role." }] Roles = [new RoleModel { Name = "User", Description = "User role" }],
Groups = [new GroupModel() { Name = "Administrators", Description = "Administrators group",
Roles = [new RoleModel() { Name = "Admin", Description = "Administrator" },
new RoleModel() { Name = "Test", Description = "Test role" } ]
}]
}); });
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"}); service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
}; };