Skip to content

Commit dd571b8

Browse files
committed
ResourceApi Infrastructure & Sample Improvement
1 parent 7f941a5 commit dd571b8

40 files changed

+759
-158
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi
2+
@inject ProductApiService Api
3+
4+
<MudDialog Class="mud-width-full" ContentClass="uauth-dialog">
5+
6+
<TitleContent>
7+
<MudText>Resource Api</MudText>
8+
<MudText Typo="Typo.subtitle2" Color="Color.Primary">Sample demonstration of a resource.</MudText>
9+
</TitleContent>
10+
11+
<DialogContent>
12+
Products: @(string.Join(", ", _products.Select(x => x.Name)))
13+
<MudButton OnClick="GetProducts">Get Products</MudButton>
14+
</DialogContent>
15+
16+
</MudDialog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using CodeBeam.UltimateAuth.Client;
2+
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
3+
using Microsoft.AspNetCore.Components;
4+
using MudBlazor;
5+
6+
namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.Components.Dialogs;
7+
8+
public partial class ResourceApiDialog
9+
{
10+
[CascadingParameter]
11+
private IMudDialogInstance MudDialog { get; set; } = default!;
12+
13+
[Parameter]
14+
public UAuthState AuthState { get; set; } = default!;
15+
16+
List<Product> _products = new List<Product>();
17+
18+
private async Task GetProducts()
19+
{
20+
_products = await Api.GetAllAsync();
21+
}
22+
}

samples/blazor-standalone-wasm/CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm/Pages/Home.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@
142142
</MudItem>
143143
</MudGrid>
144144
}
145+
146+
<MudStack Class="mt-4" Row="true" AlignItems="AlignItems.Center">
147+
<MudText Typo="Typo.subtitle2">Resource Api</MudText>
148+
<MudDivider Style="width: 60%" />
149+
</MudStack>
150+
151+
<MudButton FullWidth Variant="Variant.Outlined" StartIcon="@Icons.Material.Filled.Api" OnClick="OpenResourceApiDialog">
152+
<MudText Class="mud-width-full" Align="Align.Center">Manage Resource</MudText>
153+
</MudButton>
145154
</MudStack>
146155
</MudExpansionPanel>
147156
</MudItem>

samples/blazor-standalone-wasm/CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm/Pages/Home.razor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ private async Task OpenRoleDialog()
190190
await DialogService.ShowAsync<RoleDialog>("Role Management", GetDialogParameters(), UAuthDialog.GetDialogOptions());
191191
}
192192

193+
private async Task OpenResourceApiDialog()
194+
{
195+
await DialogService.ShowAsync<ResourceApiDialog>("Resource Api", GetDialogParameters(), UAuthDialog.GetDialogOptions());
196+
}
197+
193198
private DialogParameters GetDialogParameters()
194199
{
195200
return new DialogParameters

samples/blazor-standalone-wasm/CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm/Program.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using CodeBeam.UltimateAuth.Core.Extensions;
44
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm;
55
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.Infrastructure;
6+
using CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
67
using Microsoft.AspNetCore.Components.Web;
78
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
89
using MudBlazor.Services;
@@ -28,16 +29,21 @@
2829
});
2930
builder.Services.AddMudExtensions();
3031

32+
builder.Services.AddScoped<ProductApiService>();
3133
builder.Services.AddScoped<DarkModeManager>();
3234

33-
//builder.Services.AddHttpClient("UAuthHub", client =>
34-
//{
35-
// client.BaseAddress = new Uri("https://localhost:6110");
36-
//});
3735

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

41+
builder.Services.AddScoped(sp =>
42+
{
43+
return new HttpClient
44+
{
45+
BaseAddress = new Uri("https://localhost:6120") // Resource API
46+
};
47+
});
48+
4349
await builder.Build().RunAsync();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
2+
3+
public class Product
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; } = "";
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Microsoft.AspNetCore.Components.WebAssembly.Http;
2+
using System.Net.Http.Json;
3+
4+
namespace CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm.ResourceApi;
5+
6+
public class ProductApiService
7+
{
8+
private readonly HttpClient _http;
9+
10+
public ProductApiService(HttpClient http)
11+
{
12+
_http = http;
13+
}
14+
15+
public async Task<List<Product>> GetAllAsync()
16+
{
17+
var request = new HttpRequestMessage(HttpMethod.Get, "/api/products");
18+
request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);
19+
20+
var response = await _http.SendAsync(request);
21+
22+
if (!response.IsSuccessStatusCode)
23+
throw new Exception($"Failed to fetch products: {response.StatusCode}");
24+
25+
return await response.Content.ReadFromJsonAsync<List<Product>>() ?? new();
26+
}
27+
28+
public async Task<Product?> GetAsync(int id)
29+
{
30+
var response = await _http.GetAsync($"/api/products/{id}");
31+
32+
if (!response.IsSuccessStatusCode)
33+
return null;
34+
35+
return await response.Content.ReadFromJsonAsync<Product>();
36+
}
37+
38+
public async Task<Product?> CreateAsync(Product product)
39+
{
40+
var response = await _http.PostAsJsonAsync("/api/products", product);
41+
42+
if (!response.IsSuccessStatusCode)
43+
throw new Exception($"Create failed: {response.StatusCode}");
44+
45+
return await response.Content.ReadFromJsonAsync<Product>();
46+
}
47+
48+
public async Task DeleteAsync(int id)
49+
{
50+
var response = await _http.DeleteAsync($"/api/products/{id}");
51+
52+
if (!response.IsSuccessStatusCode)
53+
throw new Exception($"Delete failed: {response.StatusCode}");
54+
}
55+
}

samples/blazor-standalone-wasm/CodeBeam.UltimateAuth.Sample.BlazorStandaloneWasm/wwwroot/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<base href="/" />
99
<link rel="stylesheet" href="css/app.css" />
1010
<link rel="icon" type="image/png" href="UltimateAuth-Logo.png" />
11-
<link href="UltimateAuth.Sample.BlazorStandaloneWasm.styles.css" rel="stylesheet" />
12-
1311
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
1412
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
1513
<link href="_content/CodeBeam.MudBlazor.Extensions/MudExtensions.min.css" rel="stylesheet" />

samples/resource-api/CodeBeam.UltimateAuth.Sample.ResourceApi/Controllers/WeatherForecastController.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

samples/resource-api/CodeBeam.UltimateAuth.Sample.ResourceApi/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
policy
2020
.WithOrigins("https://localhost:6130")
2121
.AllowAnyHeader()
22-
.AllowAnyMethod();
22+
.AllowAnyMethod()
23+
.AllowCredentials();
2324
});
2425
});
2526

2627
var app = builder.Build();
2728

28-
// Configure the HTTP request pipeline.
2929
if (app.Environment.IsDevelopment())
3030
{
3131
app.MapOpenApi();
@@ -34,6 +34,7 @@
3434
app.UseHttpsRedirection();
3535
app.UseCors("WasmSample");
3636

37+
app.UseUltimateAuthResourceApi();
3738
app.UseAuthentication();
3839
app.UseAuthorization();
3940

0 commit comments

Comments
 (0)