text: from datetime import datetime, timedelta
  from app import db, login, bcrypt, app
  from flask_login import UserMixin
  import jwt
  from werkzeug.security import generate_password_hash, check_password_hash
  
  class User(UserMixin, db.Model):
      __tablename__ = 'user'
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(64), index=True, unique=True)
      email = db.Column(db.String(120), unique=False)
      password_hash = db.Column(db.String(128), nullable=False)
      lastlogin = db.Column(db.DateTime, nullable=True, unique=False)
      numlogins = db.Column(db.Integer, nullable=True, unique=False)
      #posts = db.relationship('Post', backref='author', lazy='dynamic')
  
      def __init__(self, username, password):
         self.password_hash = bcrypt.generate_password_hash(
          password, app.config.get('BCRYPT_LOG_ROUNDS')
      ).decode()
         self.username = username
  
  
      def encode_auth_token(self, user_id):
          """
          Generates the Auth Token
          :return: string
          """
          try:
              payload = {
                  'exp': datetime.utcnow() + timedelta(days=0, seconds=10800),
                  'iat': datetime.utcnow(),
                  'sub': user_id
              }
              return jwt.encode(
                  payload,
                  app.config.get('SECRET_KEY'),
                  algorithm='HS256'
              )
          except Exception as e:
              return e
  
      @staticmethod
      def decode_auth_token(auth_token):
          """
          Validates the auth token
          :param auth_token:
          :return: integer|string
          """
          try:
              payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'))
              return payload['sub']
          except jwt.ExpiredSignatureError:
              return 'Signature expired. Please log in again.'
          except jwt.InvalidTokenError:
              return 'Invalid token. Please log in again.'
  
      def __repr__(self):
          return '<User {}>'.format(self.username)
  
      #def set_password(self, password):
      #    self.password_hash = generate_password_hash(password)
  
      def check_password(self, password):
          return check_password_hash(self.password_hash, password)
  
  
  @login.user_loader
  def load_user(id):
      return User.query.get(int(id))
  