From 48f21d4533065169f34bd0bdb71b7bb9b627ee75 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 14 Oct 2024 03:19:39 +0200 Subject: [PATCH] Auth system --- .../Components/Pages/Auth/Login.razor | 18 +++++++++ SharpRSS.Blazor/Components/Redirector.razor | 29 ++++++++++++++ SharpRSS.Blazor/Components/Routes.razor | 36 +++++++++++------- .../Extensions/NavigationManagerExtensions.cs | 14 +++++++ SharpRSS.Blazor/Program.cs | 6 ++- SharpRSS.Blazor/SRSS.db-shm | Bin 0 -> 32768 bytes SharpRSS.Blazor/SRSS.db-wal | 0 7 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 SharpRSS.Blazor/Components/Pages/Auth/Login.razor create mode 100644 SharpRSS.Blazor/Components/Redirector.razor create mode 100644 SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs create mode 100644 SharpRSS.Blazor/SRSS.db-shm create mode 100644 SharpRSS.Blazor/SRSS.db-wal diff --git a/SharpRSS.Blazor/Components/Pages/Auth/Login.razor b/SharpRSS.Blazor/Components/Pages/Auth/Login.razor new file mode 100644 index 0000000..9ae2b97 --- /dev/null +++ b/SharpRSS.Blazor/Components/Pages/Auth/Login.razor @@ -0,0 +1,18 @@ +@page "/Auth/Login" +@using SharpRSS.Blazor.Extensions +@inject NavigationManager NavigationManager +@attribute [AllowAnonymous] + +Login | SharpRSS + +@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(); + } +} \ No newline at end of file diff --git a/SharpRSS.Blazor/Components/Redirector.razor b/SharpRSS.Blazor/Components/Redirector.razor new file mode 100644 index 0000000..7f59098 --- /dev/null +++ b/SharpRSS.Blazor/Components/Redirector.razor @@ -0,0 +1,29 @@ +@inject NavigationManager NavigationManager + +Redirecting... + + + + + + +@code { + + [CascadingParameter] + private Task 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(); + } + +} \ No newline at end of file diff --git a/SharpRSS.Blazor/Components/Routes.razor b/SharpRSS.Blazor/Components/Routes.razor index a977289..0705dc9 100644 --- a/SharpRSS.Blazor/Components/Routes.razor +++ b/SharpRSS.Blazor/Components/Routes.razor @@ -1,13 +1,23 @@ - - - - - - - - @*TODO: Manage not found*@ - Not found! -

Page not found!

-
-
-
\ No newline at end of file + + + + @* + *@ + + +

Determining session state, please wait...

+
+ + + +
+
+ + + @*TODO: Manage not found*@ + Not found! +

Page not found!

+
+
+
+
\ No newline at end of file diff --git a/SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs b/SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs new file mode 100644 index 0000000..5721a4e --- /dev/null +++ b/SharpRSS.Blazor/Extensions/NavigationManagerExtensions.cs @@ -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 GetQueryParameters(this NavigationManager navigationManager) + { + var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri); + return QueryHelpers.ParseQuery(uri.Query); + } +} \ No newline at end of file diff --git a/SharpRSS.Blazor/Program.cs b/SharpRSS.Blazor/Program.cs index f9dd1d9..9ea8342 100644 --- a/SharpRSS.Blazor/Program.cs +++ b/SharpRSS.Blazor/Program.cs @@ -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() { 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(); options.SetSessionStateProviderType(); diff --git a/SharpRSS.Blazor/SRSS.db-shm b/SharpRSS.Blazor/SRSS.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 GIT binary patch literal 32768 zcmeIuAr62r3