예제 #1
0
        public IActionResult Login(UserForLoginDto userForRegisterDto)
        {
            var userFromRepo = _repo.Login(userForRegisterDto.Username, userForRegisterDto.Password);

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }

            var key = new SymmetricSecurityKey(Encoding.UTF8
                                               .GetBytes(_configuration.GetSection("AppSettings:Token").Value));

            var tokenHandeler = new JwtSecurityTokenHandler();
            var token         = JwtTokenHelper.CreateJwtToken(key, userFromRepo.ID, userFromRepo.Username);
            var user          = _mapper.Map <EmployeeAfterSuccessfulLoginDto>(userFromRepo);

            return(Ok(new
            {
                token = tokenHandeler.WriteToken(token),
                user
            }));
        }