diff --git a/DotBased b/DotBased index 8531079..58739c2 160000 --- a/DotBased +++ b/DotBased @@ -1 +1 @@ -Subproject commit 8531079a16df9bd10c305d22075d7a135f8f8878 +Subproject commit 58739c2aeadc7e608359a20cd3ada2d447284c22 diff --git a/SharpRSS.Blazor/Components/App.razor b/SharpRSS.Blazor/Components/App.razor index 268e07d..961400a 100644 --- a/SharpRSS.Blazor/Components/App.razor +++ b/SharpRSS.Blazor/Components/App.razor @@ -12,12 +12,12 @@ @*MudBlazor*@ - + @*Rendermode to 'InteractiveServer' or else MudBlazor will not work with interactivity*@ - + @*MudBlazor*@ diff --git a/SharpRSS.Blazor/Components/Pages/Application/Login.razor b/SharpRSS.Blazor/Components/Pages/Application/Login.razor index 735d694..d0829c3 100644 --- a/SharpRSS.Blazor/Components/Pages/Application/Login.razor +++ b/SharpRSS.Blazor/Components/Pages/Application/Login.razor @@ -1,56 +1,65 @@ @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 Login | SharpRSS - - - - - - - - - - - Login - - - - - + + + + + + + + + SharpRSS login + + + + + + + Login + + + + + + + @code { + [CascadingParameter] + private Task 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); } - - Snackbar.Add(loginResult.Message, Severity.Error); } } \ No newline at end of file diff --git a/SharpRSS.Blazor/Components/_Imports.razor b/SharpRSS.Blazor/Components/_Imports.razor index a7f89ce..b964b43 100644 --- a/SharpRSS.Blazor/Components/_Imports.razor +++ b/SharpRSS.Blazor/Components/_Imports.razor @@ -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 diff --git a/SharpRSS.Blazor/Program.cs b/SharpRSS.Blazor/Program.cs index a89f38c..6391f9f 100644 --- a/SharpRSS.Blazor/Program.cs +++ b/SharpRSS.Blazor/Program.cs @@ -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"}); };