Compare commits

...

2 Commits

7 changed files with 53 additions and 22 deletions

View File

@@ -3,21 +3,14 @@ using Sandbox.Models.ViewModels;
namespace Sandbox.App.Services.AuthService;
public class AuthService : IAuthService
public class AuthService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider)
: IAuthService
{
private readonly HttpClient _http;
//private readonly AuthenticationStateProvider _authenticationStateProvider;
// public AuthService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider)
public AuthService(HttpClient httpClient)
{
_http = httpClient;
//_authenticationStateProvider = authenticationStateProvider;
}
private readonly AuthenticationStateProvider _authenticationStateProvider = authenticationStateProvider;
public async Task<ServiceResponse<int>> Register(UserRegister request)
{
var result = await _http.PostAsJsonAsync("api/auth/register", request);
var result = await httpClient.PostAsJsonAsync("api/auth/register", request);
return await result.Content.ReadFromJsonAsync<ServiceResponse<int>>();
}

View File

@@ -15,7 +15,7 @@
</head>
<body>
<Routes />
<Routes @rendermode="InteractiveServer" />
<ReconnectModal />
<script src="@Assets["_framework/blazor.web.js"]"></script>
</body>

View File

@@ -1,13 +1,13 @@
@page "/register"
@using Blazored.LocalStorage
@using Sandbox.Models.ViewModels
@inject Sandbox.App.Services.AuthService.IAuthService AuthService
@* @inject AuthenticationStateProvider AuthenticationStateProvider *@
@* @inject NavigationManager NavigationManager *@
@inject ILocalStorageService LocalStorageService
<title>Register</title>
<EditForm Model="user" OnValidSubmit="HandleRegistration" OnInvalidSubmit="HandleInvalidSubmit" FormName="registerForm">
<EditForm Model="user" OnValidSubmit="HandleRegistration" OnInvalidSubmit="HandleInvalidSubmit">
<DataAnnotationsValidator/>
<div class="d-flex justify-content-center align-items-center">
<div class="col-md-4 p-5 shadow-sm border rounded-3">
@@ -41,6 +41,10 @@
</div>
</EditForm>
<div class="text-info">
@RendererInfo.Name
</div>
<div class="text-danger">
<span>@errorMessage</span>
</div>
@@ -50,9 +54,7 @@
</div>
@code {
[SupplyParameterFromForm] private UserRegister user { get; set; }
//HttpClient _http = new() { BaseAddress = new("https://localhost:7122") };
private UserRegister user { get; set; }
private string message = string.Empty;
private string messageCssClass = string.Empty;
@@ -62,11 +64,11 @@
async Task HandleRegistration()
{
// var response = await _http.PostAsJsonAsync("/api/register", user);
// var result = await response.Content.ReadFromJsonAsync<ServiceResponse<int>>();
var result = await AuthService.Register(user);
message = result.Message;
messageCssClass = result.Success ? "text-success" : "text-danger";
await LocalStorageService.SetItemAsync("test", message);
}
async Task HandleInvalidSubmit()

View File

@@ -2,7 +2,12 @@
using Microsoft.AspNetCore.Hosting.Server.Features;
namespace Sandbox;
/// <summary>
/// Source: https://www.duracellko.net/posts/2020/06/hosting-both-blazor-server-and-webassembly
/// </summary>
/// <param name="httpClient"></param>
/// <param name="server"></param>
/// <param name="applicationLifetime"></param>
public class HttpClientSetupService(
HttpClient httpClient,
IServer server,

View File

@@ -1,9 +1,9 @@
using Blazored.LocalStorage;
using Sandbox;
using Sandbox.Components;
using Sandbox.Models.ViewModels;
using Sandbox.Services.AuthService;
using ClientServices = Sandbox.App.Services;
using ServerServices = Sandbox.Services;
var builder = WebApplication.CreateBuilder(args);
@@ -11,6 +11,9 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Blazor.Storage
builder.Services.AddBlazoredLocalStorage();
// Blazor client services
builder.Services.AddScoped<ClientServices.AuthService.IAuthService, ClientServices.AuthService.AuthService>();

View File

@@ -9,4 +9,8 @@
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazor.Storage" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "blazor-server-mini-sandbox", "blazor-server-mini-sandbox.csproj", "{A4595977-F945-77FD-CAC9-1B945F061E97}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A4595977-F945-77FD-CAC9-1B945F061E97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4595977-F945-77FD-CAC9-1B945F061E97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4595977-F945-77FD-CAC9-1B945F061E97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4595977-F945-77FD-CAC9-1B945F061E97}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08319A16-FD23-406A-9429-7EA3AFE8A9DD}
EndGlobalSection
EndGlobal