Skip to content

Commit 8e4df66

Browse files
Add validation and error handling
1 parent ddb4a80 commit 8e4df66

File tree

6 files changed

+41
-11
lines changed

6 files changed

+41
-11
lines changed

Modules/Authentication.Web/Controllers/BaseController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace GenHTTP.Modules.Authentication.Web.Controllers
1111
public class BaseController
1212
{
1313

14-
protected IHandlerBuilder RenderAccountEntry(string title, string buttonCaption)
14+
protected IHandlerBuilder RenderAccountEntry(string title, string buttonCaption, string? username = null, string? errorMessage = null)
1515
{
16-
return ModRazor.Page(Resource.FromAssembly("EnterAccount.cshtml"), (r, h) => new ViewModel<EnterAccountModel>(r, h, new(buttonCaption)))
16+
return ModRazor.Page(Resource.FromAssembly("EnterAccount.cshtml"), (r, h) => new ViewModel<EnterAccountModel>(r, h, new(buttonCaption, username, errorMessage)))
1717
.AddStyle("{web-auth-resources}/style.css")
1818
.Title(title);
1919
}

Modules/Authentication.Web/Controllers/LoginController.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> perform
2424
public IHandlerBuilder Index()
2525
{
2626
// ToDo: already logged in
27-
return RenderAccountEntry("Login", "Login");
27+
return RenderLogin();
2828
}
2929

3030
[ControllerAction(RequestMethod.POST)]
3131
public async Task<IHandlerBuilder> Index(string user, string password, IRequest request)
3232
{
33+
if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(password))
34+
{
35+
return RenderLogin(user, "Please enter username and password.");
36+
}
37+
3338
var authenticatedUser = await PerformLogin(request, user, password);
3439

3540
if (authenticatedUser != null)
@@ -40,10 +45,12 @@ public async Task<IHandlerBuilder> Index(string user, string password, IRequest
4045
}
4146
else
4247
{
43-
return RenderAccountEntry("Login", "Login");
48+
return RenderLogin(user, "Invalid username or password.");
4449
}
4550
}
4651

52+
private IHandlerBuilder RenderLogin(string? username = null, string? errorMessage = null) => RenderAccountEntry("Login", "Login", username, errorMessage);
53+
4754
}
4855

4956
}

Modules/Authentication.Web/Controllers/SetupController.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
using System.Threading.Tasks;
33

44
using GenHTTP.Api.Content;
5-
using GenHTTP.Api.Content.Templating;
65
using GenHTTP.Api.Protocol;
76

87
using GenHTTP.Modules.Basics;
98
using GenHTTP.Modules.Controllers;
10-
using GenHTTP.Modules.IO;
11-
using GenHTTP.Modules.Razor;
129

1310
namespace GenHTTP.Modules.Authentication.Web.Controllers
1411
{
@@ -25,17 +22,24 @@ public SetupController(Func<IRequest, string, string, ValueTask> performSetup)
2522

2623
public IHandlerBuilder Index()
2724
{
28-
return RenderAccountEntry("Setup", "Create Account");
25+
return RenderSetup();
2926
}
3027

3128
[ControllerAction(RequestMethod.POST)]
3229
public async Task<IHandlerBuilder> Index(string user, string password, IRequest request)
3330
{
31+
if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(password))
32+
{
33+
return RenderSetup(user, "Please enter username and password.");
34+
}
35+
3436
await PerformSetup(request, user, password);
3537

3638
return Redirect.To("{web-auth}/", true);
3739
}
3840

41+
private IHandlerBuilder RenderSetup(string? username = null, string? errorMessage = null) => RenderAccountEntry("Setup", "Create Account", username, errorMessage);
42+
3943
}
4044

4145
}

Modules/Authentication.Web/Resources/style.css

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
flex-flow: column wrap;
2222
gap: 20px;
2323
margin-left: 10px;
24+
margin-right: 15px;
2425
}
2526

2627
.user_cont, .pass_cont {
@@ -37,7 +38,6 @@
3738
}
3839

3940
button {
40-
width: 100%;
4141
border: none;
4242
outline: none;
4343
padding: 5px 8px;
@@ -46,6 +46,14 @@ button {
4646
background-color: #DEDEDE;
4747
}
4848

49+
.error_message {
50+
background-color: #C5532F;
51+
padding: 8px;
52+
border-radius: 10px;
53+
font-style: italic;
54+
font-size: 10pt;
55+
}
56+
4957
button:hover {
5058
transform: translateY(-1px);
5159
cursor: pointer;

Modules/Authentication.Web/ViewModels/EnterAccountModel.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
public record EnterAccountModel
55
(
66

7-
string ButtonCaption
7+
string ButtonCaption,
8+
9+
string? Username = null,
10+
11+
string? ErrorMessage = null
812

913
);
1014

Modules/Authentication.Web/Views/EnterAccount.cshtml

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="inputs">
66

77
<div class="user_cont">
8-
<input type="text" id="user" name="user" placeholder="username" />
8+
<input type="text" id="user" name="user" placeholder="username" value="@Model.Data.Username" />
99
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="#333333" class="icon w-6 h-6">
1010
<path stroke-linecap="round" d="M16.5 12a4.5 4.5 0 11-9 0 4.5 4.5 0 019 0zm0 0c0 1.657 1.007 3 2.25 3S21 13.657 21 12a9 9 0 10-2.636 6.364M16.5 12V8.25" />
1111
</svg>
@@ -17,6 +17,13 @@
1717
<path stroke-linecap="round" stroke-linejoin="round" d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z" />
1818
</svg>
1919
</div>
20+
21+
@if (@Model.Data.ErrorMessage != null)
22+
{
23+
<div class="error_message">
24+
@Model.Data.ErrorMessage
25+
</div>
26+
}
2027

2128
<button>@Model.Data.ButtonCaption</button>
2229

0 commit comments

Comments
 (0)