Example #1
0
        public async Task <IActionResult> Login(UserForLoginDto userForLoginDto)
        {
            var userFromRepo = await _userManager.FindByNameAsync(userForLoginDto.UserName);

            if (userFromRepo == null)
            {
                return(Unauthorized(new ReturnMessage()
                {
                    Status = false,
                    Title = Resource.ErrorMessages.Error,
                    Message = Resource.ErrorMessages.WrongEmailOrPassword,
                    Code = "401"
                }));
            }
            var result = await _signInManager.CheckPasswordSignInAsync(userFromRepo, userForLoginDto.Password, false);

            if (result.Succeeded)
            {
                var appUser = _userManager.Users.Include(p => p.Photos)
                              .FirstOrDefault(u => u.NormalizedUserName == userForLoginDto.UserName.ToUpper());

                var userForReturn = _mapper.Map <UserDetailDto>(appUser);

                _logger.LogInformation($"user {userFromRepo.Name} - {userFromRepo.Id} logged in. ");

                return(Ok(new
                {
                    token = await _utilities.GenerateJwtTokenAsync(appUser, userForLoginDto.IsRemember),
                    userForReturn
                }));
            }

            else
            {
                return(Unauthorized(new ReturnMessage()
                {
                    Status = false,
                    Title = Resource.ErrorMessages.Error,
                    Message = Resource.ErrorMessages.WrongEmailOrPassword,
                    Code = "401"
                }));
            }
        }