0

I am storing userid in token, i am authenticating the token and then after getting user id validating the user is enabled or not in mysql db. So for other tables i need userid for db queries as well, so how can i get user id in those functions? is there a way to get payload and fetch the the identity. Main.py

app.secret_key = "temp"
api = Api(app)
jwt = JWT(app,authenticate, identity) #/auth automatically

Security.py

def authenticate(username, password):
    user = User.find_by_username(username)
    if user and user.password == password:
        return user


def identity(payload):
    user_id = payload["identity"]
    return User.find_by_userid(user_id)

User file


def find_by_userid(cls,_id):
        #db connection
        query = "select uid from users where uid= %s"

message file

#i want user id in this function as well
def fetch_msg_(cls,uid):

  • I'm wondering why you are checking the token user against the database. The whole idea of JWTs is to prevent exactly that external validation overhead. – Klaus D. Oct 15 '20 at 06:57
  • I have db where i have implemented different tables, i am storing user id in token, so when user get authenticated then i am fetching the data related to user id, now if user wants to see messages then it will send other request with token, and now to fetch all messages related to particular user i need user id. I guess this is clear. – ashish zarekar Oct 15 '20 at 07:16

0 Answers0