I implemented a server side Session in Flask with SQLAlchemy based on this snippit:
class SqlAlchemySession(CallbackDict, SessionMixing):
...
class SqlAlchemySessionInterface(SessionInterface):
def __init__(self, db):
self.db = db
def open_session(self, app, request):
...
def save_session(self, app, session, response):
...
Everything works as expected. When the user logs in, a session is stored in the database, and the session id is placed in a cookie and returned to the user. When the user logs out, session.clear()
is called, and the cookie is removed from the user.
However, the session is not deleted from the database. I was hoping that I could implement this logic in my SqlAlchemySessionInterface
class, as opposed to defining a function and calling this instead of session.clear()
.
Likewise, in the sessions.py code, there isn't any reference to clear
, and the only time a cookie is deleted is if the session was modified.
The API documentation for sessions also doesn't indicate how the clear
method works.
Would anyone know of a way of accomplishing this, other than replacing all my calls to session.clear()
with:
def clear_session():
sid = session.get('sid')
if sid:
db.session.query(DBSession).filter_by(sid=sid).delete()
db.session.commit()
session.clear()