title: Flask Python Authentication error: identity = getattr(identity, 'id') or identity['id']
body: <p>I am trying a simple authentication logic using Flask and flask_jwt, it used to work normally but all of a sudden I started getting this exception:
  <code>identity = getattr(identity, 'id') or identity['id']</code></p>
  <p>here is my user model:</p>
  <pre><code>class UserModel:
      def __init__(self, id, username, password):
          self._id = id
          self.username = username
          self.password = password
  
      @classmethod
      def find_by_username(cls, username):
          db = sqlite3.connect(path)
          cursor = db.cursor()
          query = 'SELECT * FROM users WHERE username=?'
          result = cursor.execute(query, (username,))
          row = result.fetchone()
          if row:
              user = cls(*row)
          else:
              user = None
          db.close()
          return user
  
      @classmethod
      def find_by_id(cls, _id):
          db = sqlite3.connect(path)
          cursor = db.cursor()
          query = 'SELECT * FROM users WHERE id=?'
          result = cursor.execute(query, (_id,))
          row = result.fetchone()
          if row:
              user = cls(*row)
          else:
              user = None
          db.close()
          return user
  </code></pre>
  <p>here is my security module:</p>
  <pre><code>def authenticate(username, password):
      print(&quot;authenticate was called.&quot;)
      user = UserModel.find_by_username(username)
      print(user)
      if user and safe_str_cmp(user.password, password):
          return user
  
  def identity(payload):
      user = payload['identity']
      return UserModel.find_by_id(user)
  </code></pre>
  <p>I can't figure out what is causing this exception, I am aware that there are more sophisticated ways to go about this, but I am building on my app from a simple rudimentary one to a more in line with current practices one, here is the stacktrace:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\EM\PycharmProjects\Learn\venv\lib\site-packages\flask\app.py&quot;, line 2464, in __call__
      return self.wsgi_app(environ, start_response)
    ...
    File &quot;C:\Users\EM\PycharmProjects\Learn\venv\lib\site-packages\flask_jwt\__init__.py&quot;, line 125, in _default_auth_request_handler
      access_token = _jwt.jwt_encode_callback(identity)
    File &quot;C:\Users\EM\PycharmProjects\Learn\venv\lib\site-packages\flask_jwt\__init__.py&quot;, line 62, in _default_jwt_encode_handler
      payload = _jwt.jwt_payload_callback(identity)
    File &quot;C:\Users\EM\PycharmProjects\Learn\venv\lib\site-packages\flask_jwt\__init__.py&quot;, line 53, in _default_jwt_payload_handler
      identity = getattr(identity, 'id') or identity['id']
  AttributeError: 'UserModel' object has no attribute 'id'
  </code></pre>