public Task <TokenDto> Handle(RegisterUserCommand request, CancellationToken cancellationToken) { return(_applicationUserDxo.Map(request) .MapAsync(async user => { var identityResult = await _userManager.CreateAsync(user, request.Password); return (user, identityResult); }) .Map(tuple => tuple.identityResult.Succeeded ? EitherAsync <IdentityResult, ApplicationUser> .Right(tuple.user) : EitherAsync <IdentityResult, ApplicationUser> .Left(tuple.identityResult)) .MapAsyncT(user => _jwtService.GenerateSecurityToken(user)) .MapT(s => _tokenDxo.Map(s)) .Match(async => async.Match( dto => { return dto.Match(tokenDto => tokenDto.AsTask(), exception => exception.AsFailedTask <TokenDto>()); }, result => { return new ArgumentException(string.Join(",", result.Errors.Select(error => error.Description))) .AsFailedTask <TokenDto>(); }), exception => exception.AsFailedTask <TokenDto>()) .Flatten()); }
public Task <TokenDto> Handle(SignInCommand request, CancellationToken cancellationToken) { return(TryAsync(async() => { var user = await _userManager.FindByEmailAsync(request.Email); var identityResult = await _userManager.CheckPasswordAsync(user, request.Password); return (user, identityResult); }).Map(tuple => tuple.identityResult ? EitherAsync <bool, ApplicationUser> .Right(tuple.user) : EitherAsync <bool, ApplicationUser> .Left(tuple.identityResult)) .MapAsyncT(user => _jwtService.GenerateSecurityToken(user)) .MapT(s => _tokenDxo.Map(s)) .Match(async => async.Match( dto => { return dto.Match(tokenDto => tokenDto.AsTask(), exception => exception.AsFailedTask <TokenDto>()); }, result => new ArgumentException("Bad password") .AsFailedTask <TokenDto>()), exception => exception.AsFailedTask <TokenDto>()) .Flatten()); }