mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-11-14 09:34:21 +01:00
Auth system
This commit is contained in:
parent
bc619c62cb
commit
48f21d4533
18
SharpRSS.Blazor/Components/Pages/Auth/Login.razor
Normal file
18
SharpRSS.Blazor/Components/Pages/Auth/Login.razor
Normal 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();
|
||||
}
|
||||
}
|
29
SharpRSS.Blazor/Components/Redirector.razor
Normal file
29
SharpRSS.Blazor/Components/Redirector.razor
Normal 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,23 @@
|
|||
<Router AppAssembly="typeof(Program).Assembly">
|
||||
<Found Context="routeData">
|
||||
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
|
||||
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
|
||||
</Found>
|
||||
<NotFound>
|
||||
<LayoutView Layout="@typeof(EmptyLayout)">
|
||||
@*TODO: Manage not found*@
|
||||
<PageTitle>Not found!</PageTitle>
|
||||
<p>Page not found!</p>
|
||||
</LayoutView>
|
||||
</NotFound>
|
||||
</Router>
|
||||
<CascadingAuthenticationState>
|
||||
<Router AppAssembly="typeof(Program).Assembly">
|
||||
<Found Context="routeData">
|
||||
@*<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)">
|
||||
@*TODO: Manage not found*@
|
||||
<PageTitle>Not found!</PageTitle>
|
||||
<p>Page not found!</p>
|
||||
</LayoutView>
|
||||
</NotFound>
|
||||
</Router>
|
||||
</CascadingAuthenticationState>
|
14
SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs
Normal file
14
SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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
BIN
SharpRSS.Blazor/SRSS.db-shm
Normal file
Binary file not shown.
0
SharpRSS.Blazor/SRSS.db-wal
Normal file
0
SharpRSS.Blazor/SRSS.db-wal
Normal file
Loading…
Reference in New Issue
Block a user