Added CORS, added specific type for token in AuthToken.cs, added login page to blazor client
This commit is contained in:
		| @@ -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> | ||||
|   | ||||
| @@ -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!; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user