diff --git a/DotBased b/DotBased index d98634d..8531079 160000 --- a/DotBased +++ b/DotBased @@ -1 +1 @@ -Subproject commit d98634d8887e0bab7add7f2181c2cdd5db77e1d2 +Subproject commit 8531079a16df9bd10c305d22075d7a135f8f8878 diff --git a/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs b/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs deleted file mode 100644 index b65b3ba..0000000 --- a/SharpRSS.Blazor/Auth/LocalStorageSessionStateProvider.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Blazored.LocalStorage; -using DotBased; -using DotBased.ASP.Auth; -using DotBased.Extensions; -using DotBased.Logging; -using Serilog; -using ILogger = DotBased.Logging.ILogger; - -namespace SharpRSS.Blazor.Auth; - -public class LocalStorageSessionStateProvider : ISessionStateProvider -{ - public LocalStorageSessionStateProvider(ILocalStorageService localStorage) - { - _localStorage = localStorage; - _logger = LogService.RegisterLogger(typeof(LocalStorageSessionStateProvider)); - } - private readonly ILocalStorageService _localStorage; - private readonly ILogger _logger; - - public async Task> GetSessionStateAsync() - { - var localStorageValue = await _localStorage.GetItemAsync(ISessionStateProvider.SessionStateName); - if (localStorageValue != null && !localStorageValue.IsNullOrWhiteSpace()) - return Result.Ok(localStorageValue); - _logger.Warning("Failed to get session token from local storage!"); - return Result.Failed("Local storage returned null or empty on session token."); - } - - public async Task SetSessionStateAsync(string state) - { - if (state.IsNullOrWhiteSpace()) - { - Log.Warning("Tried to save empty or null session state to local storage!"); - return Result.Failed("Could not set session state to local storage, value is empty or null!"); - } - await _localStorage.SetItemAsync(ISessionStateProvider.SessionStateName, state); - return Result.Ok(); - } -} \ No newline at end of file diff --git a/SharpRSS.Blazor/Components/App.razor b/SharpRSS.Blazor/Components/App.razor index c969d9a..268e07d 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 4ea1d84..735d694 100644 --- a/SharpRSS.Blazor/Components/Pages/Application/Login.razor +++ b/SharpRSS.Blazor/Components/Pages/Application/Login.razor @@ -1,17 +1,56 @@ @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 + + + + + + @code { private string RedirectAfterLogin { get; set; } = string.Empty; + LoginModel model = new(); protected override Task OnInitializedAsync() { - RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : string.Empty; + RedirectAfterLogin = NavigationManager.GetQueryParameters().TryGetValue("RedirectUrl", out var redirectUrl) ? redirectUrl.ToString() : "/"; //TODO: Checking based auth or external (OIDC, etc.) return base.OnInitializedAsync(); } + + private async void ValidSubmit(EditContext obj) + { + var loginResult = await SecurityService.LoginAsync(model); + if (loginResult.Success && loginResult.Value != null) + { + await LocalStorage.SetAsync("dotbased_session", loginResult.Value.Id); + NavigationManager.NavigateTo(RedirectAfterLogin); + } + + Snackbar.Add(loginResult.Message, Severity.Error); + } + } \ No newline at end of file diff --git a/SharpRSS.Blazor/Components/Pages/Home.razor b/SharpRSS.Blazor/Components/Pages/Home.razor index 6e1ab80..893f538 100644 --- a/SharpRSS.Blazor/Components/Pages/Home.razor +++ b/SharpRSS.Blazor/Components/Pages/Home.razor @@ -3,6 +3,11 @@ Home Mud text! + + + Welcome: @context.User.Identity.Name + + diff --git a/SharpRSS.Blazor/Program.cs b/SharpRSS.Blazor/Program.cs index 04ccea9..a89f38c 100644 --- a/SharpRSS.Blazor/Program.cs +++ b/SharpRSS.Blazor/Program.cs @@ -1,10 +1,8 @@ -using Blazored.LocalStorage; using DotBased.ASP.Auth; using DotBased.ASP.Auth.Domains.Auth; using DotBased.ASP.Auth.Domains.Identity; using Microsoft.EntityFrameworkCore; using MudBlazor.Services; -using SharpRSS.Blazor.Auth; using SharpRSS.Blazor.Components; using SharpRSS.Business; using SharpRSS.Data; @@ -13,23 +11,21 @@ using SharpRSS.Data.Domains.Configuration; var builder = WebApplication.CreateBuilder(args); builder.AddSRSS(); -builder.Services.AddBlazoredLocalStorage(); - builder.Services.AddBasedServerAuth(options => { options.AllowRegistration = false; options.AuthenticationStateMaxAgeBeforeExpire = TimeSpan.FromDays(7); options.LoginPath = "/auth/login"; options.LogoutPath = "/auth/logout"; + options.LoggedOutPath = "/auth/loggedOut"; options.SeedData = service => { - 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 = "Admin", Name = "Administrator", FamilyName = "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(); }); // Add services to the container. diff --git a/SharpRSS.Blazor/SharpRSS.Blazor.csproj b/SharpRSS.Blazor/SharpRSS.Blazor.csproj index 51196ae..49a83a5 100644 --- a/SharpRSS.Blazor/SharpRSS.Blazor.csproj +++ b/SharpRSS.Blazor/SharpRSS.Blazor.csproj @@ -12,7 +12,6 @@ - @@ -31,4 +30,8 @@ + + + + diff --git a/SharpRSS.Business/Services/AuthService.cs b/SharpRSS.Business/Services/AuthService.cs index f85bd99..97f57f9 100644 --- a/SharpRSS.Business/Services/AuthService.cs +++ b/SharpRSS.Business/Services/AuthService.cs @@ -1,4 +1,5 @@ using DotBased; +using DotBased.ASP.Auth.Domains; using DotBased.Logging; using SharpRSS.Data.Domains.Auth; diff --git a/SharpRSS.Data/Domains/Auth/LoginModel.cs b/SharpRSS.Data/Domains/Auth/LoginModel.cs deleted file mode 100644 index 5a1044f..0000000 --- a/SharpRSS.Data/Domains/Auth/LoginModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace SharpRSS.Data.Domains.Auth; - -public class LoginModel -{ - public string UserName { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; -} \ No newline at end of file