public async Task <UserToken> Handle(SignInCommand request, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByNameAsync(request.Email);

            if (user == null)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, "User not found");
            }

            var userIsLockedOut = await _userManager.IsLockedOutAsync(user);

            if (userIsLockedOut)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, "Account has been deleted");
            }

            var result = await _userManager.CheckPasswordAsync(user, request.Password);

            if (result == false)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, "Invalid Credential");
            }

            return(_jwtTokenService.IssueToken(user));
        }
Example #2
0
        public async Task <UserToken> Handle(SignUpCommand request, CancellationToken cancellationToken)
        {
            var user = new ApplicationUser
            {
                UserName = request.Email,
                Email    = request.Email
            };

            var result = await _userManager.CreateAsync(user, request.Password);

            if (result.Succeeded == false)
            {
                var errors = result.Errors.Select(p => $"Code:{p.Code}; Description:{p.Description}");
                throw new ApplicationApiException(HttpStatusCode.BadRequest, string.Join("\n", errors));
            }

            return(_jwtTokenService.IssueToken(user));
        }