public async Task <ActionResult> Login([FromBody] LoginDto model) { var user = await _userManager.FindByEmailAsync(model.Email); if (user != null && await _userManager.CheckPasswordAsync(user, model.Password)) { var claims = new List <Claim> { new Claim(ClaimTypes.Role, Roles.User.ToString()), }; var token = _tokenService.GetToken(claims); return(Ok( new TokenWithExpirationDto(new JwtSecurityTokenHandler().WriteToken(token), token.ValidTo) )); } return(Unauthorized()); }
public async Task <IActionResult> CreateToken([FromBody] AuthenticateModel authenticate) { try { var user = await _userService.GetUserByUserNameAsync(authenticate.Username); if (user == null) { return(Ok(new Response { Status = (int)ResponseStatus.UserNotExist, Data = null, ErrorMessage = "User does not exist" })); } if (!user.IsActivated || user.RemovedOn != null) { return(Ok(new Response { Status = (int)ResponseStatus.AccountDeactivated, Data = null, ErrorMessage = "Your account has been deactivated. Please contact administrator" })); } if (Hasher.Verify(authenticate.Password, user.Password)) { var model = new UserModel(user, _jwtTokenService.GetToken(user)); return(Ok(new Response { Status = (int)ResponseStatus.Success, Data = model, ErrorMessage = null })); } return(Ok(new Response { Status = (int)ResponseStatus.UsernamePasswordNotMatch, Data = null, ErrorMessage = "Username or password does not match" })); } catch (Exception e) { Console.WriteLine(e); throw; } }