Skip to content

Commit

Permalink
#218 - Completed client-side password change.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Feb 22, 2019
1 parent 226bf19 commit 5a7fb7f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Money.UI.Backend/Commands/Handlers/UserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task HandleAsync(Envelope<ChangePassword> command)
IdentityResult result = await userManager.ChangePasswordAsync(user, command.Body.Current, command.Body.New);
if (!result.Succeeded)
{
var ex = new PasswordChangeFailedException();
var ex = new PasswordChangeFailedException(String.Join(Environment.NewLine, result.Errors.Select(e => e.Description)));
AggregateRootExceptionDecorator decorator = new AggregateRootExceptionDecorator(ex);
decorator.SetCommandKey(command.Body.Key);
decorator.SetSourceCommandKey(command.Body.Key);
Expand Down
16 changes: 15 additions & 1 deletion src/Money.UI.Blazor/Commands/PasswordChangeFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@ namespace Money.Commands
/// An exception raised when user password change fails.
/// </summary>
public class PasswordChangeFailedException : AggregateRootException
{ }
{
/// <summary>
/// Gets a user error message.
/// </summary>
public string ErrorDescription { get; }

/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="errorDescriptions">A user error message.</param>
public PasswordChangeFailedException(string errorDescription)
{
ErrorDescription = errorDescription;
}
}
}
6 changes: 6 additions & 0 deletions src/Money.UI.Blazor/Components/Bootstrap/Alert.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@

protected string ModeCssClass { get; set; }

protected override void OnInit()
{
base.OnInit();
UpdateModeCssClass();
}

protected void UpdateModeCssClass()
{
switch (mode)
Expand Down
2 changes: 2 additions & 0 deletions src/Money.UI.Blazor/Components/ExceptionPanel.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
message = MessageBuilder.CantDeleteDefaultCurrency();
else if (e is CantDeleteLastCurrencyException)
message = MessageBuilder.CantDeleteLastCurrency();
else if (e is PasswordChangeFailedException passwordChangeFailed)
message = MessageBuilder.PasswordChangeFailed(passwordChangeFailed.ErrorDescription);

Message = message;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Money.UI.Blazor/Events/PasswordChanged.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Neptuo.Events;
using Neptuo.Models.Keys;
using Neptuo.Models.Keys;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,10 +10,12 @@ namespace Money.Events
/// <summary>
/// An event raised when user password has been changed.
/// </summary>
public class PasswordChanged : Event
public class PasswordChanged : UserEvent
{
public PasswordChanged(IKey key, IKey aggregateKey)
public PasswordChanged(IKey key, IKey aggregateKey)
: base(key, aggregateKey, 0)
{ }
{
UserKey = aggregateKey;
}
}
}
8 changes: 8 additions & 0 deletions src/Money.UI.Blazor/Pages/User/ChangePassword.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<UserHead />

<div class="user">

<ExceptionPanel />

@if (IsSuccess)
{
<Alert Message="Your password has been changed." Mode="AlertMode.Success" IsDismissible="true" />
}

<div class="row">
<div class="col-md-6">
<form method="post" onsubmit="@OnFormSubmit">
Expand Down
6 changes: 6 additions & 0 deletions src/Money.UI.Blazor/Pages/User/ChangePassword.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ChangePasswordBase : BlazorComponent, IEventHandler<PasswordChanged
public string New { get; set; }
public string Confirm { get; set; }

public bool IsSuccess { get; set; }

protected override Task OnInitAsync()
{
BindEvents();
Expand All @@ -31,6 +33,8 @@ protected override Task OnInitAsync()

protected async Task OnFormSubmit()
{
IsSuccess = false;

if (!String.IsNullOrEmpty(Current) && !String.IsNullOrEmpty(New) && New == Confirm)
await Commands.HandleAsync(new Commands.ChangePassword(Current, New));
}
Expand Down Expand Up @@ -59,6 +63,8 @@ Task IEventHandler<PasswordChanged>.HandleAsync(PasswordChanged payload)
Current = null;
New = null;
Confirm = null;
IsSuccess = true;
StateHasChanged();
return Task.CompletedTask;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Money.UI.Blazor/Services/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class MessageBuilder
public string CurrencyExchangeRateAlreadyExists() => "Such currency exchange rate already exists.";
public string OutcomeAlreadyDeleted() => "The outcome is already deleted.";
public string OutcomeAlreadyHasCategory() => "The outcome already has this category.";

public string PasswordChangeFailed(string error) => error.Replace(Environment.NewLine, "<br />");
}
}
7 changes: 7 additions & 0 deletions src/Money/Events/UserEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ public class UserEvent : Event
/// Gets a key of the user.
/// </summary>
public IKey UserKey { get; /*internal*/ set; }

public UserEvent()
{ }

public UserEvent(IKey key, IKey aggregateKey, int version)
: base(key, aggregateKey, version)
{ }
}
}

0 comments on commit 5a7fb7f

Please sign in to comment.