1

So I am having trouble with JWT tokens. After my token runs trough function to decode it it is supposed to return some data from SQLAlchemy database. But instead of data I get <__main__.SortRules object at 0x1074befe0> if I return it as string or TypeError: Object of type SortRules is not JSON serializable if I just return it. I need to get this as some readable format string or JSON.

current_user = create_user in token_required - stores user ID which is taken from database

Decoder fnction for JWT:

def token_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        token = None
        local_session = Session(bind=engine)
        
        data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
        create_users = local_session.query(User).filter_by(public_id=data['public_id']).first()

        return f(create_users, *args, **kwargs)
    
    return decorated

Function where i call JWT token:

class SortRules(Resource):
    @token_required
    def post(self, current_user):

        return current_user

        OR

        return str(current_user)

api.add_resource(SortRules, '/v1/postRule')

I tried some JSON decoders I found here but it didn't help. Tried this one for example but it only returned empty JSON:

class MyEncoder(json.JSONEncoder):
        def default(self, o):
            return o.__dict__ 

MyEncoder().encode(current_user)
json.dumps(current_user ,cls=MyEncoder)

Also tried current_user.decode('utf-8') throws AttributeError: 'SortRules' object has no attribute 'decode'

So is there any way I can get what I need from JWT token in string or JSON format ?

wondergrandma
  • 103
  • 1
  • 12

0 Answers0