<commit_before>﻿using Wilcommerce.Core.Infrastructure;

namespace Wilcommerce.Auth.Events.User.Handlers
{
    public class UserEventHandler : 
        IHandleEvent<UserSignedInEvent>,
        IHandleEvent<PasswordRecoveryRequestedEvent>
    {
        public IEventStore EventStore { get; }

        public UserEventHandler(IEventStore eventStore)
        {
            EventStore = eventStore;
        }

        public void Handle(UserSignedInEvent @event)
        {
            try
            {
                EventStore.Save(@event);
            }
            catch 
            {
                throw;
            }
        }

        public void Handle(PasswordRecoveryRequestedEvent @event)
        {
            try
            {
                EventStore.Save(@event);
            }
            catch
            {
                throw;
            }
        }
    }
}<commit_msg>add token validation event to handler<commit_after>﻿using Microsoft.AspNetCore.Http.Authentication;
using System;
using System.Linq;
using Wilcommerce.Auth.Services.Interfaces;
using Wilcommerce.Core.Common.Domain.ReadModels;
using Wilcommerce.Core.Infrastructure;

namespace Wilcommerce.Auth.Events.User.Handlers
{
    public class UserEventHandler : 
        IHandleEvent<UserSignedInEvent>,
        IHandleEvent<PasswordRecoveryRequestedEvent>,
        IHandleEvent<PasswordRecoveryValidatedEvent>
    {
        public IEventStore EventStore { get; }

        public IIdentityFactory IdentityFactory { get; }

        public ICommonDatabase CommonDatabase { get; }

        /// <summary>
        /// Get the OWIN Authentication manager
        /// </summary>
        public AuthenticationManager AuthenticationManager { get; }

        public UserEventHandler(IEventStore eventStore, IIdentityFactory identityFactory, ICommonDatabase commonDatabase, AuthenticationManager authenticationManager)
        {
            EventStore = eventStore;
            IdentityFactory = identityFactory;
            CommonDatabase = commonDatabase;
            AuthenticationManager = authenticationManager;
        }

        public void Handle(UserSignedInEvent @event)
        {
            try
            {
                EventStore.Save(@event);
            }
            catch 
            {
                throw;
            }
        }

        public void Handle(PasswordRecoveryRequestedEvent @event)
        {
            try
            {
                EventStore.Save(@event);
            }
            catch
            {
                throw;
            }
        }

        public void Handle(PasswordRecoveryValidatedEvent @event)
        {
            try
            {
                EventStore.Save(@event);

                var user = CommonDatabase.Users
                    .FirstOrDefault(u => u.Id == @event.AggregateId);

                if (user == null)
                {
                    throw new Exception("User not found");
                }

                var principal = IdentityFactory.CreateIdentity(user);
                AuthenticationManager.SignInAsync(AuthenticationDefaults.AuthenticationScheme, principal);
            }
            catch
            {
                throw;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06812577065351418, 'fraction_numerical': 0.0, 'mean_word_length': 1.5233281493001556, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3521100', 'n_tokens_mistral': 769, 'n_tokens_neox': 737, 'n_words': 142}