GoodMoodProvider/APIGoodMoodProvider/APIGoodMoodProvider/Controllers/UserController.cs
/// <summary>
/// Authenticate user and put token and refresh token in database
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Authenticate user and put token and refresh token in database
Authenticate user and put token and refresh token in database
[AllowAnonymous]
        [HttpPost]
        [Route("Authenticate")]
        [EnableCors]
        public async Task<IActionResult> Authenticate(AuthenticationRequest request)
        {
            try
            {
                var users = (await _unitOfWork.UserRepository.GetAllAsync());
                var user = users.FirstOrDefault(u =>
                u.Login == request.Login);
                if(user == null)
                     return BadRequest("Incorrect login");
                if(!(user.Password == _encrypter.EncryptString(request.Password)))
                     return BadRequest("Incorrect password or login");
                var response = _userHandler.Authenticate(user.ID);
                user.Token = response.Token;
                user.RefreshToken = response.RefreshToken;
                var query = new GetUserById(user.ID);
                User userWithRoles = await  _mediator.Send(query);
                foreach(UserRole ur in user.UserRoles)
                {
                    var role = await _unitOfWork.RoleRepository.GetByIdAsync(ur.RoleID);
                    response.Roles.Add(role.Name);
                }
                await _unitOfWork.UserRepository.PutAsync(user);
                await _unitOfWork.SaveDBAsync();
                return Ok(response);
            }
            catch(Exception ex)
            {
                Log.Error($"{DateTime.Now}|Error|{ex}");    
                return BadRequest(ex);
            }
        }
[ AllowAnonymous ] [ HttpPost ] [ Route ( " " ) ] [ EnableCors ] public async Task < IActionResult > Authenticate ( AuthenticationRequest request ) { try { var users = ( await _unitOfWork . UserRepository . GetAllAsync ( ) ) ; var user = users . FirstOrDefault ( u => u . Login == request . Login ) ; if ( user == null ) return BadRequest ( " " ) ; if ( ! ( user . Password == _encrypter . EncryptString ( request . Password ) ) ) return BadRequest ( " " ) ; var response = _userHandler . Authenticate ( user . ID ) ; user . Token = response . Token ; user . RefreshToken = response . RefreshToken ; var query = new GetUserById ( user . ID ) ; User userWithRoles = await _mediator . Send ( query ) ; foreach ( UserRole ur in user . UserRoles ) { var role = await _unitOfWork . RoleRepository . GetByIdAsync ( ur . RoleID ) ; response . Roles . Add ( role . Name ) ; } await _unitOfWork . UserRepository . PutAsync ( user ) ; await _unitOfWork . SaveDBAsync ( ) ; return Ok ( response ) ; } catch ( Exception ex ) { Log . Error ( $" { DateTime . Now } |Error| { ex } " ) ; return BadRequest ( ex ) ; } }
Authenticate user and put token and refresh token in database
Authenticate user and put token and refresh token in database
None
request
AuthenticationRequest
None