diff --git a/src/Koogle.Web/Components/Shared/ClubSwitcher.razor b/src/Koogle.Web/Components/Shared/ClubSwitcher.razor index 64f962c..c637771 100644 --- a/src/Koogle.Web/Components/Shared/ClubSwitcher.razor +++ b/src/Koogle.Web/Components/Shared/ClubSwitcher.razor @@ -1,16 +1,10 @@ -@using System.Net @using Fluxor -@using Koogle.Application.DTOs @using Koogle.Web.Store.AuthState -@using Koogle.Application.Interfaces +@using Microsoft.AspNetCore.Antiforgery @inject IState AuthState -@inject NavigationManager NavigationManager -@inject IUserService UserService -@inject HttpClient HttpClient; -@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery +@inject IAntiforgery Antiforgery @inject IHttpContextAccessor HttpContextAccessor -@inject IDispatcher Dispatcher @inherits Fluxor.Blazor.Web.Components.FluxorComponent @@ -27,14 +21,24 @@ @foreach (var club in AuthState.Value.AvailableClubs) { - - @if (club.ClubId == AuthState.Value.CurrentClub?.ClubId) - { + @if (club.ClubId == AuthState.Value.CurrentClub?.ClubId) + { + - } - @club.ClubName - + @club.ClubName + + } + else + { +
+ + + + +
+ } }
@@ -53,64 +57,17 @@ else if (AuthState.Value.IsAuthenticated && AuthState.Value.HasNoClub) } @code { - private string _antiToken; + private string _token = string.Empty; - protected override void OnAfterRender(bool firstRender) + protected override void OnInitialized() { - if (firstRender) + base.OnInitialized(); + + var http = HttpContextAccessor.HttpContext; + if (http != null) { - var http = HttpContextAccessor.HttpContext!; var tokens = Antiforgery.GetAndStoreTokens(http); - _antiToken = tokens.RequestToken!; + _token = tokens?.RequestToken ?? string.Empty; } } - - private async Task SwitchClubAsync(Guid clubId) - { - if (AuthState.Value.CurrentUser == null) - return; - - var model = new SwitchClubFormDto() - { - ClubId = clubId, - UserProfileId = AuthState.Value.CurrentUser.ProfileId - }; - - try - { - HttpClient.DefaultRequestHeaders.Remove("RequestVerificationToken"); - HttpClient.DefaultRequestHeaders.Add( - "RequestVerificationToken", - _antiToken - ); - - var basepath = NavigationManager.BaseUri; - var url = $"{basepath}auth/switch-club"; - await HttpClient.PostAsJsonAsync(url, model); - - // Dispatcher.Dispatch(new AuthState.InitializeAuthSuccessAction(model.UserProfileId, model.ClubId, roles)); - - - NavigationManager.NavigateTo("/dashboard", forceLoad: true); - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - - // try - // { - // var success = await UserService.SwitchClubAsync(AuthState.Value.CurrentUser.ProfileId, clubId); - // if (success) - // { - // // Force page reload to refresh claims - // NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true); - // } - // } - // catch (Exception ex) - // { - // Console.WriteLine($"Club switch failed: {ex.Message}"); - // } - } } diff --git a/src/Koogle.Web/Controllers/AuthController.cs b/src/Koogle.Web/Controllers/AuthController.cs index 31b4958..f16f071 100644 --- a/src/Koogle.Web/Controllers/AuthController.cs +++ b/src/Koogle.Web/Controllers/AuthController.cs @@ -142,11 +142,11 @@ namespace Koogle.Web.Controllers /// Handles switch club. /// [HttpPost("switch-club")] - //[ValidateAntiForgeryToken] - public async Task SwitchClub([FromBody] SwitchClubFormDto input) + [ValidateAntiForgeryToken] + public async Task SwitchClub([FromForm] SwitchClubFormDto input) { await _userService.SwitchClubAsync(input.UserProfileId, input.ClubId); - return LocalRedirect($"/dashboard"); + return LocalRedirect("/dashboard"); } }