diff --git a/src/Money.Api/Users/Controllers/UserController.cs b/src/Money.Api/Users/Controllers/UserController.cs index 9eb5ca39..494d4c95 100644 --- a/src/Money.Api/Users/Controllers/UserController.cs +++ b/src/Money.Api/Users/Controllers/UserController.cs @@ -67,5 +67,18 @@ public async Task Login([FromBody] LoginRequest model) return BadRequest(); } + + [HttpPost] + public async Task Register([FromBody] RegisterRequest model) + { + var user = new ApplicationUser(model.UserName); + var result = await userManager.CreateAsync(user, model.Password); + + var response = new RegisterResponse(); + if (!result.Succeeded) + response.ErrorMessages.AddRange(result.Errors.Select(e => e.Description)); + + return Ok(response); + } } } diff --git a/src/Money.UI.Blazor/Models/User/RegisterRequest.cs b/src/Money.UI.Blazor/Models/User/RegisterRequest.cs new file mode 100644 index 00000000..618bc54f --- /dev/null +++ b/src/Money.UI.Blazor/Models/User/RegisterRequest.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Users.Models +{ + public class RegisterRequest + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/src/Money.UI.Blazor/Models/User/RegisterResponse.cs b/src/Money.UI.Blazor/Models/User/RegisterResponse.cs new file mode 100644 index 00000000..a6032406 --- /dev/null +++ b/src/Money.UI.Blazor/Models/User/RegisterResponse.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Users.Models +{ + public class RegisterResponse + { + public bool IsSuccess => ErrorMessages.Count == 0; + + public List ErrorMessages { get; set; } = new List(); + } +} diff --git a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs index 51590165..45959d0d 100644 --- a/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs @@ -1,5 +1,4 @@ using Microsoft.AspNetCore.Blazor.Components; -using Microsoft.AspNetCore.Blazor.Services; using Money.Services; using Neptuo.Collections.Specialized; using System; diff --git a/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs b/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs index 51a923d3..0e14988b 100644 --- a/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs +++ b/src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Blazor.Components; +using Money.Services; using System; using System.Collections.Generic; using System.Linq; @@ -9,24 +10,43 @@ namespace Money.Pages { public class RegisterBase : BlazorComponent { - public string UserName { get; set; } - public string Password { get; set; } - public string ConfirmPassword { get; set; } + [Inject] + internal ApiClient ApiClient { get; set; } + + [Inject] + internal Navigator Navigator { get; set; } + + protected string UserName { get; set; } + protected string Password { get; set; } + protected string ConfirmPassword { get; set; } public List ErrorMessages { get; } = new List(); - protected Task OnSubmitAsync() + protected async Task OnSubmitAsync() { if (Validate()) { - UserName = null; - Password = null; - ConfirmPassword = null; + var response = await ApiClient.RegisterAsync(UserName, Password); + if (response.IsSuccess) + { + if (await ApiClient.LoginAsync(UserName, Password, false)) + { + Navigator.OpenSummary(); + } + else + { + UserName = null; + Password = null; + ConfirmPassword = null; - ErrorMessages.Add("Passed ;-)"); + Navigator.OpenLogin(); + } + } + else + { + ErrorMessages.AddRange(response.ErrorMessages); + } } - - return Task.CompletedTask; } protected bool Validate() @@ -42,7 +62,7 @@ protected bool Validate() if (String.IsNullOrEmpty(ConfirmPassword)) ErrorMessages.Add("Please, fill password confirmation."); else if (Password != ConfirmPassword) - ErrorMessages.Add("Password must match password confirmation."); + ErrorMessages.Add("Passwords must match."); return ErrorMessages.Count == 0; } diff --git a/src/Money.UI.Blazor/Services/ApiClient.cs b/src/Money.UI.Blazor/Services/ApiClient.cs index d6be5cec..9e27eeea 100644 --- a/src/Money.UI.Blazor/Services/ApiClient.cs +++ b/src/Money.UI.Blazor/Services/ApiClient.cs @@ -25,6 +25,7 @@ public class ApiClient private readonly CommandMapper commandMapper; private readonly QueryMapper queryMapper; private readonly IExceptionHandler exceptionHandler; + private readonly IEventDispatcher eventDispatcher; public ApiClient(HttpClient http, CommandMapper commandMapper, QueryMapper queryMapper, IExceptionHandler exceptionHandler, IEventDispatcher eventDispatcher) @@ -93,9 +94,24 @@ public async Task LoginAsync(string userName, string password, bool isPerm return false; } - public async Task LogoutAsync() + public Task LogoutAsync() { ClearAuthorization(); + return Task.CompletedTask; + } + + public async Task RegisterAsync(string userName, string password) + { + RegisterResponse response = await http.PostJsonAsync( + "/api/user/register", + new RegisterRequest() + { + UserName = userName, + Password = password + } + ); + + return response; } private Request CreateRequest(Type type, string payload)