Skip to content

Commit 3fe2dd5

Browse files
committed
Complete Login Path with Minimum Required Features
1 parent 04fb8c0 commit 3fe2dd5

39 files changed

+670
-244
lines changed

samples/blazor-server/UltimateAuth.BlazorServer/Components/App.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<script src="_framework/blazor.web.js"></script>
2222
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
2323
<script src="_content/CodeBeam.MudBlazor.Extensions/MudExtensions.min.js"></script>
24+
<script src="_content/CodeBeam.UltimateAuth.Client/uauth.js"></script>
2425
</body>
2526

2627
</html>
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
@page "/"
2-
@inject IUAuthFlowService<UserId> FlowService
32
@inject ISnackbar Snackbar
4-
@inject IHttpClientFactory Http
53

64
<div class="uauth-page d-flex align-center justify-center">
75
<MudStack Class="uauth-stack">
8-
<form method="post" action="/auth/login">
9-
<MudText Typo="Typo.h4">Welcome to UltimateAuth!</MudText>
10-
<!-- Hidden fields -->
11-
<input type="hidden" name="Identifier" value="@_username" />
12-
<input type="hidden" name="Secret" value="@_password" />
13-
14-
<!-- UI -->
15-
<MudTextField @bind-Value="@_username" Variant="Variant.Outlined" Label="Username" Immediate="true" />
16-
<MudPasswordField @bind-Value="@_password" Variant="Variant.Outlined" Label="Password" Immediate="true" />
17-
18-
<MudButton Variant="Variant.Filled" Color="Color.Primary" ButtonType="ButtonType.Submit">Login</MudButton>
19-
</form>
6+
<UALoginForm @ref="_form" Identifier="@_username" Secret="@_password">
7+
<MudStack>
8+
<MudText Typo="Typo.h4">Welcome to UltimateAuth!</MudText>
9+
<MudTextField @bind-Value="@_username" Variant="Variant.Outlined" Label="Username" Immediate="true" />
10+
<MudPasswordField @bind-Value="@_password" Variant="Variant.Outlined" Label="Password" Immediate="true" />
11+
<MudButton Variant="Variant.Filled" Color="Color.Primary" ButtonType="ButtonType.Submit">Login</MudButton>
12+
</MudStack>
13+
</UALoginForm>
2014
</MudStack>
2115
</div>
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CodeBeam.UltimateAuth.Core.Contracts;
1+
using CodeBeam.UltimateAuth.Client.Components;
2+
using CodeBeam.UltimateAuth.Core.Contracts;
23
using Microsoft.AspNetCore.Authentication.Cookies;
34
using Microsoft.AspNetCore.Http;
45
using MudBlazor;
@@ -10,38 +11,12 @@ public partial class Home
1011
private string? _username;
1112
private string? _password;
1213

13-
private async Task LoginAsync()
14-
{
15-
16-
try
17-
{
18-
//var result = await FlowService.LoginAsync(new LoginRequest
19-
//{
20-
// Identifier = _username!,
21-
// Secret = _password!
22-
//});
23-
var client = Http.CreateClient();
24-
var result = await client.PostAsJsonAsync(
25-
"https://localhost:7213/auth/login",
26-
new LoginRequest
27-
{
28-
Identifier = _username!,
29-
Secret = _password!
30-
});
31-
14+
private UALoginForm _form;
3215

33-
if (!result.IsSuccessStatusCode)
34-
{
35-
Snackbar.Add("Login failed.", Severity.Info);
36-
return;
37-
}
38-
39-
Snackbar.Add("Successfully logged in!", Severity.Success);
40-
}
41-
catch (Exception ex)
42-
{
43-
Snackbar.Add(ex.ToString(), Severity.Error);
44-
}
16+
private async Task HandleLogin()
17+
{
18+
await _form.SubmitAsync();
4519
}
20+
4621
}
4722
}

samples/blazor-server/UltimateAuth.BlazorServer/Components/_Imports.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@using CodeBeam.UltimateAuth.Core.Abstractions
1313
@using CodeBeam.UltimateAuth.Core.Domain
14+
@using CodeBeam.UltimateAuth.Client.Components
1415

1516
@using MudBlazor
1617
@using MudExtensions

samples/blazor-server/UltimateAuth.BlazorServer/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
// Add services to the container.
1414
builder.Services.AddRazorComponents()
15-
.AddInteractiveServerComponents();
15+
.AddInteractiveServerComponents()
16+
.AddCircuitOptions(options =>
17+
{
18+
options.DetailedErrors = true;
19+
});
1620

1721
builder.Services.AddMudServices();
1822
builder.Services.AddMudExtensions();

samples/blazor-server/UltimateAuth.BlazorServer/UltimateAuth.BlazorServer.csproj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3-
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
5-
<Nullable>enable</Nullable>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="CodeBeam.MudBlazor.Extensions" Version="8.3.0" />
11-
<PackageReference Include="MudBlazor" Version="8.15.0" />
12-
</ItemGroup>
9+
<ItemGroup>
10+
<PackageReference Include="MudBlazor" Version="8.15.0" />
11+
<PackageReference Include="CodeBeam.MudBlazor.Extensions" Version="8.3.0" />
12+
</ItemGroup>
1313

1414
<ItemGroup>
15+
<ProjectReference Include="..\..\..\src\CodeBeam.UltimateAuth.Client\CodeBeam.UltimateAuth.Client.csproj" />
1516
<ProjectReference Include="..\..\..\src\CodeBeam.UltimateAuth.Core\CodeBeam.UltimateAuth.Core.csproj" />
1617
<ProjectReference Include="..\..\..\src\CodeBeam.UltimateAuth.Server\CodeBeam.UltimateAuth.Server.csproj" />
1718
<ProjectReference Include="..\..\..\src\security\CodeBeam.UltimateAuth.Security.Argon2\CodeBeam.UltimateAuth.Security.Argon2.csproj" />
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
@@ -7,4 +7,8 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
</ItemGroup>
13+
1014
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
@* TODO: Optional double-submit prevention for native form submit *@
2+
3+
@inject IJSRuntime JS
4+
5+
<form @ref="_form" method="post" action="@ResolvedEndpoint">
6+
<input type="hidden" name="Identifier" value="@Identifier" />
7+
<input type="hidden" name="Secret" value="@Secret" autocomplete="off" />
8+
9+
@ChildContent
10+
11+
@if (AllowEnterKeyToSubmit)
12+
{
13+
<button type="submit" hidden aria-hidden="true"></button>
14+
}
15+
</form>
16+
17+
@code {
18+
[Parameter]
19+
public string? Identifier { get; set; }
20+
21+
[Parameter]
22+
public string? Secret { get; set; }
23+
24+
[Parameter]
25+
public string? Endpoint { get; set; } = "/auth/login";
26+
27+
[Parameter]
28+
public RenderFragment? ChildContent { get; set; }
29+
30+
[Parameter]
31+
public bool AllowEnterKeyToSubmit { get; set; } = true;
32+
33+
private ElementReference _form;
34+
35+
private string ResolvedEndpoint => string.IsNullOrWhiteSpace(Endpoint) ? "/auth/login" : Endpoint;
36+
37+
public async Task SubmitAsync()
38+
{
39+
if (_form.Context is null)
40+
throw new InvalidOperationException("Form is not yet rendered. Call SubmitAsync after OnAfterRender.");
41+
42+
await JS.InvokeVoidAsync("uauth.submitForm", _form);
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@using Microsoft.JSInterop
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
window.uauth = {
2+
submitForm: function (form) {
3+
if (form) {
4+
form.submit();
5+
}
6+
}
7+
};

0 commit comments

Comments
 (0)