title: Token based authentication in Flask
body: <p>I am trying to create token for logged in user for a particular duration and using g.user to keep track of current user. </p>
  
  <p><code>generate_auth_token</code> and <code>verify_auth_token</code> belong to class User.</p>
  
  <p>However I keep getting this error: </p>
  
  <blockquote>
    <p>g.user.generate_auth_token(600)                                                                          'dict' object has no attribute 'generate_auth_token'</p>
  </blockquote>
  
  <pre><code>def generate_auth_token(self, expiration=600):
      s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
      return s.dumps({'id': self.id})
  
  @staticmethod
  def verify_auth_token(token):
      s = Serializer(app.config['SECRET_KEY'])
      try:
          data = s.loads(token)
      except SignatureExpired:
          return None  # valid token, but expired
      except BadSignature:
          return None  # invalid token
      user = users.find_one({'username': userdata['username']})['_id']
      return user
  
  @auth.login_required
  def get_token():
      token = g.user.generate_auth_token(600)
      return json.dumps({'token': token.decode('ascii'), 'duration': 600})
  
  @auth.verify_password
  def verify_password(username_or_token, password):
      user = User.verify_auth_token(username_or_token)
      if not user:
          user = users.find_one({'username': username_or_token})
          if not user or not pwd_context.verify(password, user['password']):
              return False
      g.user = user
      return True
  </code></pre>
  
  <p>What is the mistake I made and what's the best way to do token authentication in flask for pymongo?</p>