fzsdi
fzsdi

Reputation: 23

How to retrieve logged user id in flask?

I'm trying to make a web application with login functionality. After successful login, the user should add their request to the database with a SQL command:

db.execute(
            'INSERT INTO dailyLeave (dLeaveDateStart, dLeaveDateEnd, dComment, pId) '
            'VALUES (?, ?, ?, ?, ?, ?)',
            (dLeaveDateStart, dLeaveDateEnd, dComment, pId)

But I need a user id to put it as pId in the database. How can I get logged user id ?

They are in separate blueprints (APIs).

Upvotes: 1

Views: 4952

Answers (1)

Dinko Pehar
Dinko Pehar

Reputation: 6071

Save Id in session when the user logs in. Then you can retrieve it from the session.

First, you need to ensure that secret key is set at beginning of your Flask instance like so:

app = Flask(__name__)
app.secret_key = 'any random string’
...

When user logs in:

session['id'] = id

When you want to retrieve id, use session like python dictionary:

pId = session['id']

When user logs out:

session.pop('id')

Upvotes: 1

Related Questions