Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UltimateAuth.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<File Path="Roadmap.md" />
</Folder>
<Folder Name="/Tests/">
<Project Path="tests/CodeBeam.UltimateAuth.Tests.Integration/CodeBeam.UltimateAuth.Tests.Integration.csproj" Id="fe34a0b1-8038-400d-b8ab-02dad7051f2d" />
<Project Path="tests/CodeBeam.UltimateAuth.Tests.Unit/CodeBeam.UltimateAuth.Tests.Unit.csproj" Id="6f4b22da-849a-4a79-b5c5-aee7cb1429a6" />
</Folder>
<Project Path="src/authentication/CodeBeam.UltimateAuth.Authentication.EntityFrameworkCore/CodeBeam.UltimateAuth.Authentication.EntityFrameworkCore.csproj" Id="a8d758ad-052e-4331-9bf7-280ea9a55981" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi
@inject ProductApiService Api
@inject ISnackbar Snackbar

<MudDialog Class="mud-width-full" ContentClass="uauth-dialog">

<TitleContent>
<MudText>Resource Api</MudText>
<MudText Typo="Typo.subtitle2" Color="Color.Primary">Sample demonstration of a resource.</MudText>
</TitleContent>

<DialogContent>
<MudButton OnClick="GetProducts">Reload</MudButton>

<MudDataGrid Items="@_products" Dense="true" Striped="true" Hover="true"
EditMode="DataGridEditMode.Form" ReadOnly="false" CommittedItemChanges="@CommittedItemChanges">
<Columns>
<PropertyColumn Property="x => x.Id" Title="Id" Editable="false" />
<PropertyColumn Property="x => x.Name" Title="Name" />
<TemplateColumn Title="Actions" Editable="false">
<CellTemplate>
<MudStack Style="min-width: 120px" Row="true" Spacing="2" Wrap="Wrap.Wrap">
<MudTooltip Text="Edit" Color="Color.Primary" Delay="300" ShowOnFocus="false">
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@context.Actions.StartEditingItemAsync" />
</MudTooltip>

<MudTooltip Text="Delete" Color="Color.Error" Delay="300" ShowOnFocus="false">
<MudIconButton Color="Color.Error" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Delete" Size="Size.Small" OnClick="@(() => DeleteProduct(context.Item.Id))" />
</MudTooltip>
</MudStack>
</CellTemplate>
</TemplateColumn>
</Columns>
<PagerContent>
<MudDataGridPager T="SampleProduct" PageSizeOptions="new int[] { 5, 10, 20 }" />
</PagerContent>
</MudDataGrid>

<MudExpansionPanels Class="mt-4" Elevation="0">
<MudExpansionPanel Style="background: var(--mud-palette-background-gray)" Text="Add New Product" Expanded="false">
<MudGrid>
<MudItem xs="12" sm="6">
<MudTextField @bind-Value="_newName" Variant="Variant.Outlined" Label="Name" />
</MudItem>

<MudItem xs="12">
<MudButton Style="width: fit-content" Color="Color.Primary" Variant="Variant.Outlined" OnClick="CreateProduct">Add</MudButton>
</MudItem>
</MudGrid>
</MudExpansionPanel>
</MudExpansionPanels>
</DialogContent>

</MudDialog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using CodeBeam.UltimateAuth.Client;
using CodeBeam.UltimateAuth.Core.Contracts;
using CodeBeam.UltimateAuth.Core.Domain;
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
using CodeBeam.UltimateAuth.Users.Contracts;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.Components.Dialogs;

public partial class ResourceApiDialog
{
[CascadingParameter]
private IMudDialogInstance MudDialog { get; set; } = default!;

[Parameter]
public UAuthState AuthState { get; set; } = default!;

private List<SampleProduct> _products = new List<SampleProduct>();
private string? _newName = null;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_products = (await Api.GetAllAsync()).Value ?? new();
StateHasChanged();
}
}

private async Task<DataGridEditFormAction> CommittedItemChanges(SampleProduct item)
{
var result = await Api.UpdateAsync(item.Id, item);

if (result.IsSuccess)
{
Snackbar.Add("Product updated successfully", Severity.Success);
}
else
{
Snackbar.Add(result?.ErrorText ?? "Failed to update product.", Severity.Error);
}

return DataGridEditFormAction.Close;
}

private async Task GetProducts()
{
var result = await Api.GetAllAsync();

if (result.IsSuccess)
{
_products = result.Value ?? new();
}
else
{
Snackbar.Add(result.ErrorText ?? "Process failed.", Severity.Error);
}
}

private async Task CreateProduct()
{
var product = new SampleProduct
{
Name = _newName
};

var result = await Api.CreateAsync(product);

if (result.IsSuccess)
{
Snackbar.Add("New product created.");
_products = (await Api.GetAllAsync()).Value ?? new();
}
else
{
Snackbar.Add(result.ErrorText ?? "Process failed.", Severity.Error);
}
}

private async Task DeleteProduct(int id)
{
var result = await Api.DeleteAsync(id);

if (result.IsSuccess)
{
Snackbar.Add("Product deleted succesfully.", Severity.Success);
_products = (await Api.GetAllAsync()).Value ?? new();
}
else
{
Snackbar.Add(result.ErrorText ?? "Process failed.", Severity.Error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@
</MudItem>
</MudGrid>
}

<MudStack Class="mt-4" Row="true" AlignItems="AlignItems.Center">
<MudText Typo="Typo.subtitle2">Resource Api</MudText>
<MudDivider Style="width: 60%" />
</MudStack>

<MudButton FullWidth Variant="Variant.Outlined" StartIcon="@Icons.Material.Filled.Api" OnClick="OpenResourceApiDialog">
<MudText Class="mud-width-full" Align="Align.Center">Manage Resource</MudText>
</MudButton>
</MudStack>
</MudExpansionPanel>
</MudItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ private async Task OpenRoleDialog()
await DialogService.ShowAsync<RoleDialog>("Role Management", GetDialogParameters(), UAuthDialog.GetDialogOptions());
}

private async Task OpenResourceApiDialog()
{
await DialogService.ShowAsync<ResourceApiDialog>("Resource Api", GetDialogParameters(), UAuthDialog.GetDialogOptions());
}

private DialogParameters GetDialogParameters()
{
return new DialogParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CodeBeam.UltimateAuth.Core.Extensions;
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm;
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.Infrastructure;
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MudBlazor.Services;
Expand All @@ -28,16 +29,21 @@
});
builder.Services.AddMudExtensions();

builder.Services.AddScoped<ProductApiService>();
builder.Services.AddScoped<DarkModeManager>();

//builder.Services.AddHttpClient("UAuthHub", client =>
//{
// client.BaseAddress = new Uri("https://localhost:6110");
//});

//builder.Services.AddHttpClient("ResourceApi", client =>
//{
// client.BaseAddress = new Uri("https://localhost:6120");
//});

builder.Services.AddScoped(sp =>
{
return new HttpClient
{
BaseAddress = new Uri("https://localhost:6120") // Resource API
};
});

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CodeBeam.UltimateAuth.Core.Contracts;
using Microsoft.AspNetCore.Components.WebAssembly.Http;
using System.Net.Http.Json;

namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;

public class ProductApiService
{
private readonly HttpClient _http;

public ProductApiService(HttpClient http)
{
_http = http;
}

private HttpRequestMessage CreateRequest(HttpMethod method, string url, object? body = null)
{
var request = new HttpRequestMessage(method, url);
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);

if (body is not null)
{
request.Content = JsonContent.Create(body);
}

return request;
}

public Task<UAuthResult<List<SampleProduct>>> GetAllAsync()
=> SendAsync<List<SampleProduct>>(CreateRequest(HttpMethod.Get, "/api/products"));

public Task<UAuthResult<SampleProduct?>> GetAsync(int id)
=> SendAsync<SampleProduct?>(CreateRequest(HttpMethod.Get, $"/api/products/{id}"));

public Task<UAuthResult<SampleProduct?>> CreateAsync(SampleProduct product)
=> SendAsync<SampleProduct?>(CreateRequest(HttpMethod.Post, $"/api/products", product));

public Task<UAuthResult<SampleProduct?>> UpdateAsync(int id, SampleProduct product)
=> SendAsync<SampleProduct?>(CreateRequest(HttpMethod.Put, $"/api/products/{id}", product));

public Task<UAuthResult<SampleProduct?>> DeleteAsync(int id)
=> SendAsync<SampleProduct?>(CreateRequest(HttpMethod.Delete, $"/api/products/{id}"));

private async Task<UAuthResult<T>> SendAsync<T>(HttpRequestMessage request)
{
var response = await _http.SendAsync(request);

var result = new UAuthResult<T>
{
Status = (int)response.StatusCode,
IsSuccess = response.IsSuccessStatusCode
};

if (response.IsSuccessStatusCode)
{
result.Value = await response.Content.ReadFromJsonAsync<T>();
return result;
}

result.Problem = await TryReadProblem(response);
return result;
}

private async Task<UAuthProblem?> TryReadProblem(HttpResponseMessage response)
{
try
{
return await response.Content.ReadFromJsonAsync<UAuthProblem>();
}
catch
{
return new UAuthProblem
{
Title = response.ReasonPhrase
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;

public class SampleProduct
{
public int Id { get; set; }
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<base href="/" />
<link rel="stylesheet" href="css/app.css" />
<link rel="icon" type="image/png" href="UltimateAuth-Logo.png" />
<link href="UltimateAuth.Sample.BlazorStandaloneWasm.styles.css" rel="stylesheet" />

<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link href="_content/CodeBeam.MudBlazor.Extensions/MudExtensions.min.css" rel="stylesheet" />
Expand Down

This file was deleted.

Loading
Loading