Added CORS, added specific type for token in AuthToken.cs, added login page to blazor client

This commit is contained in:
2024-08-30 15:10:25 +03:00
parent c631dc99d0
commit 802e42563e
15 changed files with 102 additions and 36 deletions

View File

@@ -11,4 +11,8 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.1" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VisaApiClient\VisaApiClient.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,35 @@
@page "/login"
@using VisaApiClient
<PageTitle>Authentication</PageTitle>
<EditForm Model="loginData" OnValidSubmit="TryLogin">
<DataAnnotationsValidator/>
<label >Email: <InputText @bind-Value="loginData.Email"/></label>
<label >Password: <InputText @bind-Value="loginData.Password"/></label>
<input type="submit" value="Login"/>
</EditForm>
<p>@loginResult</p>
@code
{
private AuthData loginData = new();
private string loginResult = String.Empty;
[Inject]
private Client Client { get; set; } = null!;
private async Task TryLogin(EditContext obj)
{
try
{
var token = await Client.LoginAsync(loginData.Email, loginData.Password);
Client.SetAuthToken(token);
loginResult = "Logged in successfully";
}
catch (ApiException<ProblemDetails> e)
{
loginResult = e.Result.Detail!;
}
}
}

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient;
@@ -11,7 +12,13 @@ public class Program
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
//todo move to launch settings
const string baseAddress = "https://localhost:44370";
//todo make pretty
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(baseAddress) });
builder.Services.AddScoped<Client>(sp =>
new Client(baseAddress, sp.GetRequiredService<HttpClient>()));
await builder.Build().RunAsync();
}