public async Task <AuthenticateResponseDto> Authenticate(AuthenticateRequestDto model) { var user = await _context.Users.Include(user => user.Role).FirstOrDefaultAsync(u => u.Username == model.Username && u.Password == _hashingManager.GetHashedPassword(model.Password)); if (user == null) { throw new AuthenticationException("Username or password is incorrect"); } if (!user.IsActivated) { throw new AuthenticationException("Please activate your account first!"); } if ((user.WasPasswordChanged && user.WasPasswordForgotten) || (!user.WasPasswordChanged && !user.WasPasswordForgotten)) { user.WasPasswordChanged = false; _context.Users.Update(user); await _context.SaveChangesAsync(); var token = _jwtService.GenerateAuthenticationJWT(user); var authenticateResponseDto = new AuthenticateResponseDto(user, token); return(authenticateResponseDto != null ? authenticateResponseDto : throw new AuthenticationException("Username or password is incorrect")); } throw new AuthenticationException("Username or password is incorrect"); }