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*@
<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" />
<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)"/>
<HeadOutlet @rendermode="BasedAuthDefaults.InteractiveServerWithoutPrerender"/>
</head>
<body>
@*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>
@*MudBlazor*@
<script src="_content/MudBlazor/MudBlazor.min.js"></script>

View File

@ -1,22 +1,24 @@
@page "/Auth/Login"
@using DotBased.ASP.Auth.Domains
@using DotBased.ASP.Auth.Services
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@using SharpRSS.Blazor.Extensions
@using SharpRSS.Data.Domains.Auth
@inject ProtectedLocalStorage LocalStorage
@inject NavigationManager NavigationManager
@inject SecurityService SecurityService
@inject ISnackbar Snackbar
<PageTitle>Login | SharpRSS</PageTitle>
<EditForm Model="@model" OnValidSubmit="ValidSubmit">
<MudElement>
<MudOverlay DarkBackground="true" Visible="true">
<EditForm Model="@model" OnValidSubmit="ValidSubmit">
<DataAnnotationsValidator/>
<MudGrid>
<MudItem>
<MudCard>
<MudCardHeader>
<MudText>SharpRSS login</MudText>
</MudCardHeader>
<MudCardContent>
<MudTextField Label="UserName" @bind-Value="model.UserName" For="@(() => model.UserName)"/>
<MudTextField Label="Password" @bind-Value="model.Password" For="@(() => model.Password)" InputType="InputType.Password"/>
@ -27,30 +29,37 @@
</MudCard>
</MudItem>
</MudGrid>
</EditForm>
</EditForm>
</MudOverlay>
</MudElement>
@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;
LoginModel model = new();
protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : "/";
//TODO: Checking based auth or external (OIDC, etc.)
return base.OnInitializedAsync();
RedirectUrl = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : NavigationManager.BaseUri;
var authState = await AuthStateTask;
if (authState.User.Identity is { IsAuthenticated: true })
NavigationManager.NavigateTo(RedirectUrl, true);
}
private async void ValidSubmit(EditContext obj)
{
var loginResult = await SecurityService.LoginAsync(model);
if (loginResult.Success && loginResult.Value != null)
try
{
await LocalStorage.SetAsync("dotbased_session", loginResult.Value.Id);
NavigationManager.NavigateTo(RedirectAfterLogin);
}
var loginResult = await SecurityService.LoginAsync(model);
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);
}
}
}

View File

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

View File

@ -21,7 +21,11 @@ builder.Services.AddBasedServerAuth(options =>
options.SeedData = service =>
{
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"});
};