public async Task <IActionResult> Login([FromBody] ViewModel.AuthUser viewModelUser) { var entityUser = _userService.GetBy(x => x.Email == viewModelUser.Email).FirstOrDefault(); if (entityUser == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } if (!_tokenService.VerifyPassword(viewModelUser, entityUser)) { return(BadRequest(new { message = "Username or password is incorrect" })); } ResponseDto.AuthUser authUser = _mapper.Map <ResponseDto.AuthUser>(entityUser); authUser.AccessToken = _tokenService.CreateAccessToken(entityUser); var refreshToken = _tokenService.CreateRefreshToken(entityUser); authUser.RefreshToken = refreshToken; entityUser.RefreshToken = refreshToken; _userService.Update(entityUser); var state = await _userService.Commit(); if (!state) { return(StatusCode(500, "A problem occurred while handling your request")); } return(Ok(authUser)); }
public IActionResult RefreshToken(string token) { var entityUser = _userService.GetBy(x => x.RefreshToken == token).FirstOrDefault(); if (entityUser == null) { return(BadRequest(new { message = "Invalid Token" })); } ResponseDto.AuthUser authUser = _mapper.Map <ResponseDto.AuthUser>(entityUser); authUser.AccessToken = _tokenService.CreateAccessToken(entityUser); return(Ok(authUser)); }