Skip to content

Commit

Permalink
#218 - Backend for user registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 12, 2019
1 parent 0727706 commit 7c6d629
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
13 changes: 13 additions & 0 deletions src/Money.Api/Users/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,18 @@ public async Task<IActionResult> Login([FromBody] LoginRequest model)

return BadRequest();
}

[HttpPost]
public async Task<IActionResult> 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);
}
}
}
14 changes: 14 additions & 0 deletions src/Money.UI.Blazor/Models/User/RegisterRequest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
15 changes: 15 additions & 0 deletions src/Money.UI.Blazor/Models/User/RegisterResponse.cs
Original file line number Diff line number Diff line change
@@ -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<string> ErrorMessages { get; set; } = new List<string>();
}
}
1 change: 0 additions & 1 deletion src/Money.UI.Blazor/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Services;
using Money.Services;
using Neptuo.Collections.Specialized;
using System;
Expand Down
42 changes: 31 additions & 11 deletions src/Money.UI.Blazor/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Blazor.Components;
using Money.Services;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -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<string> ErrorMessages { get; } = new List<string>();

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()
Expand All @@ -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;
}
Expand Down
18 changes: 17 additions & 1 deletion src/Money.UI.Blazor/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -93,9 +94,24 @@ public async Task<bool> LoginAsync(string userName, string password, bool isPerm
return false;
}

public async Task LogoutAsync()
public Task LogoutAsync()
{
ClearAuthorization();
return Task.CompletedTask;
}

public async Task<RegisterResponse> RegisterAsync(string userName, string password)
{
RegisterResponse response = await http.PostJsonAsync<RegisterResponse>(
"/api/user/register",
new RegisterRequest()
{
UserName = userName,
Password = password
}
);

return response;
}

private Request CreateRequest(Type type, string payload)
Expand Down

0 comments on commit 7c6d629

Please sign in to comment.