Auth system

This commit is contained in:
max 2024-10-14 03:19:39 +02:00
parent bc619c62cb
commit 48f21d4533
7 changed files with 88 additions and 15 deletions

View File

@ -0,0 +1,18 @@
@page "/Auth/Login"
@using SharpRSS.Blazor.Extensions
@inject NavigationManager NavigationManager
@attribute [AllowAnonymous]
<PageTitle>Login | SharpRSS</PageTitle>
@code {
private string RedirectAfterLogin { get; set; } = string.Empty;
protected override Task OnInitializedAsync()
{
RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : string.Empty;
//TODO: Checking based auth or external (OIDC, etc.)
return base.OnInitializedAsync();
}
}

View File

@ -0,0 +1,29 @@
@inject NavigationManager NavigationManager
<PageTitle>Redirecting...</PageTitle>
<MudElement>
<MudOverlay DarkBackground="true" Visible="true">
<MudProgressCircular Size="Size.Large" Color="Color.Primary" Indeterminate="true"/>
</MudOverlay>
</MudElement>
@code {
[CascadingParameter]
private Task<AuthenticationState> AuthStateTask { get; set; } = null!;
[Parameter]
[EditorRequired]
public string RedirectUri { get; set; }
private bool _isAuthenticated;
protected override async Task OnInitializedAsync()
{
var authState = await AuthStateTask;
if (authState.User.Identity != null) _isAuthenticated = authState.User.Identity.IsAuthenticated;
NavigationManager.NavigateTo(RedirectUri);
await base.OnInitializedAsync();
}
}

View File

@ -1,7 +1,16 @@
<Router AppAssembly="typeof(Program).Assembly">
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
@*<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
<FocusOnNavigate RouteData="routeData" Selector="h1"/>*@
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(EmptyLayout)">
<Authorizing>
<p>Determining session state, please wait...</p>
</Authorizing>
<NotAuthorized>
<Redirector RedirectUri="/auth/login"/>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(EmptyLayout)">
@ -11,3 +20,4 @@
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

View File

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Primitives;
namespace SharpRSS.Blazor.Extensions;
public static class NavigationManagerExtensions
{
public static Dictionary<string, StringValues> GetQueryParameters(this NavigationManager navigationManager)
{
var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
return QueryHelpers.ParseQuery(uri.Query);
}
}

View File

@ -23,8 +23,10 @@ builder.Services.AddBasedServerAuth(options =>
options.LogoutPath = "/auth/logout";
options.SeedData = service =>
{
/*service.CreateUserAsync(new UserModel() { UserName = "Admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", Roles = new List<RoleModel>() { new RoleModel() { Name = "Admin", Description = "Administration role." }}});
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});*/
service.CreateUserAsync(new UserModel() { UserName = "Admin", Email = "admin@example.com", Enabled = true, PasswordHash = "password", Roles =
[new RoleModel { Name = "Admin", Description = "Administration role." }]
});
service.CreateUserAsync(new UserModel() { UserName = "User", Email = "user@example.com", Enabled = true, PasswordHash = "password"});
};
options.SetDataRepositoryType<MemoryAuthDataRepository>();
options.SetSessionStateProviderType<LocalStorageSessionStateProvider>();

BIN
SharpRSS.Blazor/SRSS.db-shm Normal file

Binary file not shown.

View File