Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

FacebookAuthenticationEventConsumer.cs

using System.Security.Claims;
using Nop.Services.Authentication.External;
using Nop.Services.Customers;
using Nop.Services.Events;

namespace Nop.Plugin.ExternalAuth.Facebook.Infrastructure;

/// 
/// Facebook authentication event consumer (used for saving customer fields on registration)
/// 
public class FacebookAuthenticationEventConsumer : IConsumer
{
    #region Fields

    protected readonly ICustomerService _customerService;

    #endregion

    #region Ctor

    public FacebookAuthenticationEventConsumer(ICustomerService customerService)
    {
        _customerService = customerService;
    }

    #endregion

    #region Methods

    /// 
    /// Handle event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(CustomerAutoRegisteredByExternalMethodEvent eventMessage)
    {
        if (eventMessage?.Customer == null || eventMessage.AuthenticationParameters == null)
            return;

        //handle event only for this authentication method
        if (!eventMessage.AuthenticationParameters.ProviderSystemName.Equals(FacebookAuthenticationDefaults.SystemName))
            return;

        var customer = eventMessage.Customer;
        //store some of the customer fields
        var firstName = eventMessage.AuthenticationParameters.Claims?.FirstOrDefault(claim => claim.Type == ClaimTypes.GivenName)?.Value;
        if (!string.IsNullOrEmpty(firstName))
            customer.FirstName = firstName;

        var lastName = eventMessage.AuthenticationParameters.Claims?.FirstOrDefault(claim => claim.Type == ClaimTypes.Surname)?.Value;
        if (!string.IsNullOrEmpty(lastName))
            customer.LastName = lastName;

        await _customerService.UpdateCustomerAsync(customer);
    }

    #endregion
}