|
1 |
| -using System; |
2 |
| -using System.Threading.Tasks; |
3 |
| - |
4 |
| -using GenHTTP.Api.Content; |
5 |
| -using GenHTTP.Api.Content.Authentication; |
6 |
| -using GenHTTP.Api.Protocol; |
7 |
| - |
8 |
| -using GenHTTP.Modules.Basics; |
9 |
| -using GenHTTP.Modules.Controllers; |
10 |
| - |
11 |
| -namespace GenHTTP.Modules.Authentication.Web.Controllers |
12 |
| -{ |
13 |
| - |
14 |
| - public class LoginController : BaseController |
15 |
| - { |
16 |
| - |
17 |
| - private Func<IRequest, string, string, ValueTask<IUser?>> PerformLogin { get; } |
18 |
| - |
19 |
| - public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> performLogin) |
20 |
| - { |
21 |
| - PerformLogin = performLogin; |
22 |
| - } |
23 |
| - |
24 |
| - public IHandlerBuilder Index() |
25 |
| - { |
26 |
| - // ToDo: already logged in |
27 |
| - return RenderLogin(status: ResponseStatus.Unauthorized); |
28 |
| - } |
29 |
| - |
30 |
| - [ControllerAction(RequestMethod.POST)] |
31 |
| - public async Task<IHandlerBuilder> Index(string user, string password, IRequest request) |
32 |
| - { |
33 |
| - if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(password)) |
34 |
| - { |
35 |
| - return RenderLogin(user, "Please enter username and password.", status: ResponseStatus.BadRequest); |
36 |
| - } |
37 |
| - |
38 |
| - var authenticatedUser = await PerformLogin(request, user, password); |
39 |
| - |
40 |
| - if (authenticatedUser != null) |
41 |
| - { |
42 |
| - request.SetUser(authenticatedUser); |
43 |
| - |
44 |
| - return Redirect.To("{web-auth}/", true); |
45 |
| - } |
46 |
| - else |
47 |
| - { |
48 |
| - return RenderLogin(user, "Invalid username or password.", status: ResponseStatus.Forbidden); |
49 |
| - } |
50 |
| - } |
51 |
| - |
52 |
| - private IHandlerBuilder RenderLogin(string? username = null, string? errorMessage = null, ResponseStatus? status = null) |
53 |
| - => RenderAccountEntry("Login", "Login", username, errorMessage, status); |
54 |
| - |
55 |
| - } |
56 |
| - |
57 |
| -} |
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | + |
| 4 | +using GenHTTP.Api.Content; |
| 5 | +using GenHTTP.Api.Content.Authentication; |
| 6 | +using GenHTTP.Api.Protocol; |
| 7 | + |
| 8 | +using GenHTTP.Modules.Basics; |
| 9 | +using GenHTTP.Modules.Controllers; |
| 10 | + |
| 11 | +namespace GenHTTP.Modules.Authentication.Web.Controllers |
| 12 | +{ |
| 13 | + |
| 14 | + public class LoginController |
| 15 | + { |
| 16 | + |
| 17 | + private Func<IRequest, string, string, ValueTask<IUser?>> PerformLogin { get; } |
| 18 | + |
| 19 | + public LoginController(Func<IRequest, string, string, ValueTask<IUser?>> performLogin) |
| 20 | + { |
| 21 | + PerformLogin = performLogin; |
| 22 | + } |
| 23 | + |
| 24 | + public IHandlerBuilder Index() |
| 25 | + { |
| 26 | + // ToDo: already logged in |
| 27 | + return RenderLogin(status: ResponseStatus.Unauthorized); |
| 28 | + } |
| 29 | + |
| 30 | + [ControllerAction(RequestMethod.POST)] |
| 31 | + public async Task<IHandlerBuilder> Index(string user, string password, IRequest request) |
| 32 | + { |
| 33 | + if (string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(password)) |
| 34 | + { |
| 35 | + return RenderLogin(user, "Please enter username and password.", status: ResponseStatus.BadRequest); |
| 36 | + } |
| 37 | + |
| 38 | + var authenticatedUser = await PerformLogin(request, user, password); |
| 39 | + |
| 40 | + if (authenticatedUser != null) |
| 41 | + { |
| 42 | + request.SetUser(authenticatedUser); |
| 43 | + |
| 44 | + return Redirect.To("{web-auth}/", true); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + return RenderLogin(user, "Invalid username or password.", status: ResponseStatus.Forbidden); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static IHandlerBuilder RenderLogin(string? username = null, string? errorMessage = null, ResponseStatus? status = null) |
| 53 | + => View.RenderAccountEntry("Login", "Login", username, errorMessage, status); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments