Flask Session loses login session data on refresh

Viewed 327

I'm currently building a Chat App using Flask and Flask Socketio. In order to save the client's current chat-room, I use the flask_session module with the session type "filesystem". I also use Flask-Login module so the client is logged in with an account. So when I log the client in, the session contains the client's login data from the Flask-Login module. But as soon as I refresh the chat page, the Flask_login data is gone and the current user variable from the Flask-Login module becomes an AnonymousMixin object which causes my program to crash. So what causes the FileSystemSession to lose the current_user data after refreshing the page?

This is the output when I print the session and the current_user before and after the refresh of the page:

Before:

<FileSystemSession {'_permanent': True, 'csrf_token': 'f687c55dbf48c0b9cd1e4601729ba687f74e255b', 'current_room': 'Lobby', '_fresh': True, '_user_id': '1', '_id': '4825ca5a26d88cfc91bcb75ef854505f0d4cdc736affaac2fd3211fb6015b34490c6ec47ec7175dee531442ec8d3a1d5f092c4de1349ca0d48fe723aed678c5a', '_flashes': [('success', 'Logged in successfully')]}>
<User 1>

After:

<FileSystemSession {'_permanent': True, 'csrf_token': 'f687c55dbf48c0b9cd1e4601729ba687f74e255b', 'current_room': 'Lobby'}>
<flask_login.mixins.AnonymousUserMixin object at 0x000002CD3CFB6910>

Here's my code:

__init__.py

from flask import Flask
from flask_session import Session
from flask_login import LoginManager
from .models import *
from os import path

app = Flask(__name__)

def create_app():
    app.config["SECRET_KEY"] = "secret!"
    app.config["SESSION_TYPE"] = "filesystem"
    app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_NAME}"

    login_manager = LoginManager()
    login_manager.init_app(app)

    Session(app)

    @login_manager.user_loader
    def load_user(id):
        return User.query.get(int(id))

    db.init_app(app)

    from .application import socketio

    from .views import views
    from .auth import auth

    app.register_blueprint(views, url_prefix="/")
    app.register_blueprint(auth, url_prefix="/")


    return socketio, app

views.py

from flask import Blueprint, render_template, flash, redirect, url_for, request, session
from flask_login import current_user, login_required

views = Blueprint("views", __name__)

@views.route("/", methods=["GET", "POST"])
def index():
    return render_template("index.html", user=current_user)

@views.route("/chat", methods=["GET", "POST"])
def chat():
    return render_template("chat.html", user=current_user, rooms=current_user.rooms, current_room=Room.query.filter_by(room_name=session["current_room"]).first())

I don't know if this is important but I also put in the flask_socketio part, which changes the session when the room is changed:

from flask import session
from flask_socketio import SocketIO, emit, send, join_room, leave_room
from app import app

socketio = SocketIO(app, manage_session=False)

@socketio.on('join')
def on_join(data):
    user = data["username"]
    room = data["room"]
    join_room(room)
    session["current_room"] = room
    emit("room-manager", {"message": f"{user} has joined the {room} room"}, room=room)
0 Answers
Related