diff --git a/src/Ubik.Accounting.WebApp/Security/UserService.cs b/src/Ubik.Accounting.WebApp/Security/UserService.cs index 28b010b6..47e3d8bd 100644 --- a/src/Ubik.Accounting.WebApp/Security/UserService.cs +++ b/src/Ubik.Accounting.WebApp/Security/UserService.cs @@ -1,4 +1,6 @@ -using System.Security.Claims; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.Server.Circuits; +using System.Security.Claims; using Ubik.Security.Contracts.Users.Results; namespace Ubik.Accounting.WebApp.Security @@ -63,52 +65,51 @@ internal void SetUser(ClaimsPrincipal user) } } - //TODO:Check, I think this ting is never used - //internal sealed class UserCircuitHandler( - // AuthenticationStateProvider authenticationStateProvider, - // UserService userService) : CircuitHandler, IDisposable - //{ - // private readonly AuthenticationStateProvider authenticationStateProvider = authenticationStateProvider; - // private readonly UserService userService = userService; - - // public override Task OnCircuitOpenedAsync(Circuit circuit, - // CancellationToken cancellationToken) - // { - // authenticationStateProvider.AuthenticationStateChanged += - // AuthenticationChanged; - - // return base.OnCircuitOpenedAsync(circuit, cancellationToken); - // } - - // private void AuthenticationChanged(Task task) - // { - // _ = UpdateAuthentication(task); - - // async Task UpdateAuthentication(Task task) - // { - // try - // { - // var state = await task; - // userService.SetUser(state.User); - // } - // catch - // { - // } - // } - // } - - // public override async Task OnConnectionUpAsync(Circuit circuit, - // CancellationToken cancellationToken) - // { - // var state = await authenticationStateProvider.GetAuthenticationStateAsync(); - // userService.SetUser(state.User); - // } - - // public void Dispose() - // { - // authenticationStateProvider.AuthenticationStateChanged -= - // AuthenticationChanged; - // } - //} + internal sealed class UserCircuitHandler( + AuthenticationStateProvider authenticationStateProvider, + UserService userService) : CircuitHandler, IDisposable + { + private readonly AuthenticationStateProvider authenticationStateProvider = authenticationStateProvider; + private readonly UserService userService = userService; + + public override Task OnCircuitOpenedAsync(Circuit circuit, + CancellationToken cancellationToken) + { + authenticationStateProvider.AuthenticationStateChanged += + AuthenticationChanged; + + return base.OnCircuitOpenedAsync(circuit, cancellationToken); + } + + private void AuthenticationChanged(Task task) + { + _ = UpdateAuthentication(task); + + async Task UpdateAuthentication(Task task) + { + try + { + var state = await task; + userService.SetUser(state.User); + } + catch + { + } + } + } + + public override async Task OnConnectionUpAsync(Circuit circuit, + CancellationToken cancellationToken) + { + var state = await authenticationStateProvider.GetAuthenticationStateAsync(); + userService.SetUser(state.User); + } + + public void Dispose() + { + authenticationStateProvider.AuthenticationStateChanged -= + AuthenticationChanged; + } + } } }