public async Task <IActionResult> Login(VMUser userD) { try { var user = await _userService.Authenticate(userD.Email, userD.Password); if (user == null) { ModelState.AddModelError(string.Empty, "Username and Password does not match!!!"); return(View()); } _logger.LogInfo($"User available in database."); //Get latest token from DB var userLogin = await _userLoginService.GetLatestToken(user.Id); HttpContext.Session.SetString("JWToken", userLogin.Token); var userResult = _mapper.Map <VMUser>(user); } catch (Exception ex) { _logger.LogError($"Something went wrong inside Authenticate or GetLatestToken action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } return(RedirectToAction("HomePage")); }
public async Task <IActionResult> AuthenticateUser([FromBody] VMUser userParam) { try { var user = await _userService.Authenticate(userParam.Email, userParam.Password); if (user == null) { return(BadRequest(new { message = "Email or password is incorrect" })); } _logger.LogInfo($"User available in database."); //Get latest token from DB var userLogin = await _userLoginService.GetLatestToken(user.Id); HttpContext.Session.SetString("JWToken", userLogin.Token); var userResult = _mapper.Map <VMUser>(user); return(Ok(userResult)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside Authenticate or GetLatestToken action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }