Skip to content

Commit

Permalink
Usercircuit needed for Blazor server, cannot remove this part
Browse files Browse the repository at this point in the history
  • Loading branch information
fdonnet committed Nov 15, 2024
1 parent 1b828aa commit 413aa82
Showing 1 changed file with 49 additions and 48 deletions.
97 changes: 49 additions & 48 deletions src/Ubik.Accounting.WebApp/Security/UserService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<AuthenticationState> task)
// {
// _ = UpdateAuthentication(task);

// async Task UpdateAuthentication(Task<AuthenticationState> 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<AuthenticationState> task)
{
_ = UpdateAuthentication(task);

async Task UpdateAuthentication(Task<AuthenticationState> 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;
}
}
}
}

0 comments on commit 413aa82

Please sign in to comment.