Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.4 KB
Raw Blame
using Grand.Business.Core.Events.Customers;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Domain.Customers;
using Grand.Mediator;

namespace Grand.Business.Customers.Events.Handlers;

public class CustomerLoginFailedEventHandler : INotificationHandler<CustomerLoginFailedEvent>
{
    private readonly ICustomerService _customerService;
    private readonly CustomerSettings _customerSettings;

    public CustomerLoginFailedEventHandler(ICustomerService customerService, CustomerSettings customerSettings)
    {
        _customerService = customerService;
        _customerSettings = customerSettings;
    }

    public async Task Handle(CustomerLoginFailedEvent notification, CancellationToken cancellationToken)
    {
        if (notification.Customer != null)
        {
            //wrong password
            notification.Customer.FailedLoginAttempts++;
            if (_customerSettings.FailedPasswordAllowedAttempts > 0 &&
                notification.Customer.FailedLoginAttempts >= _customerSettings.FailedPasswordAllowedAttempts)
            {
                //lock out
                notification.Customer.CannotLoginUntilDateUtc =
                    DateTime.UtcNow.AddMinutes(_customerSettings.FailedPasswordLockoutMinutes);
                //reset the counter
                notification.Customer.FailedLoginAttempts = 0;
            }

            await _customerService.UpdateCustomerLastLoginDate(notification.Customer);
        }
    }
}