How to configure mongodb for flask_session?

Viewed 349

i configured flask sessions to use mongo db to store session values but upon running the code i am getting folowing error

Traceback (most recent call last):
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 1526, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 1547, in finalize_request
    response = self.process_response(response)
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask/app.py", line 1892, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/flask_session/sessions.py", line 456, in save_session
    self.store.update({'id': store_id},
  File "/home/neo/simulation_loader/simulation2/Pratham/coding/cs50_final/django/env/lib/python3.10/site-packages/pymongo/collection.py", line 3166, in __call__
    raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'update' method on a 'Collection' object it is failing because no such method exists.

here is my code for mongo configration

# Set up database
client = pymongo.MongoClient(f"mongodb+srv://recommender:{DB_PASSWORD}@userdata.r8hvj.mongodb.net/{DB_NAME}?retryWrites=true&w=majority")
db = client.user_login_system

#session config
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "mongodb"
app.config["SESSION_MONGODB"] = client
app.config["SESSION_MONGODB_DB"] = 'userData'
app.config["SESSION_MONGODB_COLLECTION"] = 'sessions'
Session(app)

and here is how my login route functions

def login(self):
        user = db.users.find_one({"email":request.form.get("email")})
        if user:
            if pbkdf2_sha256.verify(request.form.get("password"), user["password"]):
                return self.start_session(user)
            else:
                return jsonify({"error":"Invalid password"}),400
        else:
            return jsonify({"error":"Invalid username"}),400

def start_session(self,user):
        del user["password"]
        session['logged_in'] = True
        session['user'] = user
        return jsonify(user),200
1 Answers

It seems that the current flask_session is written against an older version of pymongo. See errors you get (and I get also) seem to be related to deprecated calls on pymongo 4.X versions, see:

https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html?highlight=update_one

The logic is implemented in flask_session\sessions.py in MongoDBSessionInterface. The class seems not too complex to refactor in order to work with current pymongo (at this moment 4.1.1).

Alternative is to downgrade to older pymongo versions, like

pip uninstall pymongo
pip install pymongo==3.12.3

This is better but in my case I am getting an error related to Null expiration date (which is easier to fix), while this may also be related to my sessions configuration.

There is a pull request queued for this issue. See https://github.com/fengsp/flask-session/pull/135

Related