Skip to content

Commit

Permalink
fix(apps): show auth
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 6, 2023
1 parent 35c0d8a commit 13c7492
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
38 changes: 21 additions & 17 deletions apps/Blazor/MudBlazorUnited/Components/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
@page "/"
@using MudBlazorUnited.Data.Entities
@using System.Text.Json
@attribute [RenderModeInteractiveServer]
@inherits BaseComponent

<MudGrid>
<MudItem xs="6" sm="4">
<MudAutocomplete @ref="IdFilterAutocomplete" T="BarModel" Label="ID" SearchFunc="@SearchIdsAsync" ResetValueOnEmptyText="true"
Value="FilterList.Model"
ToStringFunc="@(e => e == null ? null : $"{e.Bar}")"
ValueChanged="@(s => ChangeIdAsync(s == null ? null : s.Id))"/>
</MudItem>

<MudItem xs="6" sm="4">
<MudTextField T="string" Value="FilterList.Title" Label="Title" ValueChanged="SearchTitleAsync" Class="mt-0" Clearable="true">
</MudTextField>
</MudItem>
<MudPageLayout Title="@LocalizationProvider["Title"]">
@if (identity is not null)
{
<MudText>@JsonSerializer.Serialize(identity)</MudText>
}
<MudGrid>
<MudItem xs="6" sm="4">
<MudAutocomplete @ref="IdFilterAutocomplete" T="BarModel" Label="ID" SearchFunc="@SearchIdsAsync" ResetValueOnEmptyText="true"
Value="FilterList.Model"
ToStringFunc="@(e => e == null ? null : $"{e.Bar}")"
ValueChanged="@(s => ChangeIdAsync(s == null ? null : s.Id))"/>
</MudItem>

<MudItem xs="12" sm="4">
<MudDateRangePicker DateRange="FilterList.DateRange" Label="Дата" PickerVariant="PickerVariant.Dialog" DateRangeChanged="@(dateRange => ChangeDateAsync(dateRange))"/>
</MudItem>
</MudGrid>
<MudItem xs="6" sm="4">
<MudTextField T="string" Value="FilterList.Title" Label="Title" ValueChanged="SearchTitleAsync" Class="mt-0" Clearable="true">
</MudTextField>
</MudItem>

<MudPageLayout Title="@LocalizationProvider["Title"]">
<MudItem xs="12" sm="4">
<MudDateRangePicker DateRange="FilterList.DateRange" Label="Дата" PickerVariant="PickerVariant.Dialog" DateRangeChanged="@(dateRange => ChangeDateAsync(dateRange))"/>
</MudItem>
</MudGrid>
<BarRepositoryList @bind-RowsPerPage="rowsPerPage" OnDataLoaded="CountSummaryAsync" @ref="barList" Class="mb-10" EnableUrlNavigation="true"
ConfigureQuery="ConfigureQueryAsync" AddParamsToUrl="AddParamsToUrlAsync"
GetParamsFromUrl="GetParamsFromUrlAsync">
Expand Down
33 changes: 33 additions & 0 deletions apps/Blazor/MudBlazorUnited/Components/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Globalization;
using System.Security.Claims;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using MudBlazor;
using MudBlazorUnited.Components.Lists;
using MudBlazorUnited.Data.Entities;
Expand Down Expand Up @@ -78,11 +80,32 @@ private async Task CountSummaryAsync(TableState state, MudTableFilter filter) =>
private MudAutocomplete<BarModel> IdFilterAutocomplete { get; set; } = null!;

[Inject] private BarRepository BarRepository { get; set; } = null!;
[Inject] private AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!;
private (BarModel[] items, int itemsCount) bars;
private User? identity;

protected override async Task InitializeAsync()
{
await base.InitializeAsync();
var authState = await AuthenticationStateProvider
.GetAuthenticationStateAsync();
var user = authState.User;

if (user.Identity is not null && user.Identity.IsAuthenticated)
{
var claimId = user.FindFirst(ClaimTypes.NameIdentifier);
if (claimId != null)
{
var flags = user.FindAll("userFlag").Select(claim => claim.Value).ToArray();
identity = new User(int.Parse(claimId.Value, CultureInfo.InvariantCulture),
user.FindFirst("userName")?.Value ?? "Anonymous",
flags.Contains(UserFlags.IsAdmin), flags,
user.FindFirst("userPic")?.Value ?? "",
user.FindFirst("name")?.Value ?? "",
user.FindFirst(ClaimTypes.Email)?.Value ?? "");
}
}

bars = await BarRepository.GetAllAsync();
}

Expand Down Expand Up @@ -187,3 +210,13 @@ public class FilterList
public DateRange? DateRange { get; set; }
}
}

public record UserIdentity(User User, string? AccessToken);
public record User(int Id, string Name, bool IsAdmin, string[] UserFlags, string? Avatar = "", string? Login = "",
string? Email = "");

public static class UserFlags
{
public const string IsAdmin = "isAdmin";
public const string IsSpecialist = "isSpecialist";
}

0 comments on commit 13c7492

Please sign in to comment.