public async Task <AuthenticationResult> LoginAsync(string email, string password)
        {
            var author = await _unitOfWork.Authors
                         .FindByConditionAsync(a => a.Email == email)
                         .Result.SingleOrDefaultAsync();

            if (author == null)
            {
                return(new AuthenticationResult
                {
                    Success = false,
                    Errors = new[] { "Author does not exist" }
                });
            }

            bool isValidPassword = SecurePasswordHasher
                                   .AreEqual(password, author.Password, author.Salt);

            if (!isValidPassword)
            {
                return(new AuthenticationResult
                {
                    Success = false,
                    Errors = new[] { "Email/password combination is invalid" }
                });
            }

            return(await TokenUtils.GenerateAuthenticationResultForUserAsync(author, _jwtOptions));
        }
Example #2
0
        public async Task ShouldBeReturnedAuthenticationResultWithSuccessAndAuthorPasswordWasReseted()
        {
            var email       = "*****@*****.**";
            var newPassword = _faker.Internet.Password();

            var authenticationResult = await _authenticationService.ResetPasswordAsync(email, newPassword);

            authenticationResult.Should().BeOfType <AuthenticationResult>();
            authenticationResult.Success.Should().BeTrue();
            authenticationResult.Token.Should().BeNull();
            authenticationResult.Errors.Should().BeNullOrEmpty();

            var resetedAuthor = await _dbContext.Authors
                                .SingleAsync(a => a.Email == email);

            bool isValidNewPassword = SecurePasswordHasher.AreEqual(newPassword,
                                                                    resetedAuthor.Password, resetedAuthor.Salt);

            isValidNewPassword.Should().BeTrue();
        }