flask-loginflask-pymongo

Flask-login current user disappearing on page refresh/redirect


I have a login route:

@bp.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        guest = Guest(**mongo.db.guests.find_one({'username': form.username.data}))
        if guest is None or not guest.check_password(form.password.data):
            flash('Invalid username or password')
            return redirect(url_for('auth.login'))
        login_user(guest, remember=form.remember_me.data)
        print(current_user)

        flash(f'Logged in {guest.name} successfully')

        next_url = request.args.get('next')
        if not next_url or url_parse(next_url).netloc != '':
            next_url = url_for('main.index')
        return redirect(next_url)

    return render_template('login.html', form=form)

When a user logins in 'successfully' it flashes and redirects the user correctly. However on the destination page (main.index or profile.view) the user is not logged in.

index route/html

@bp.route('/')
def index():
    return render_template('index.html')

{% block content %}
    <h2>Home page</h2>
    <h1>Hi, {{ current_user.username }}!</h1>
    <p><a href={{ url_for('auth.register') }}>Register</a></p>
    <p><a href={{ url_for('auth.login') }}>Sign in</a></p>
    <p><a href={{ url_for('main.index') }}>Home page</a> (accessible to anyone)</p>
    <p><a href={{ url_for('profile.view') }}>Profile View</a> (login required)</p>
    <p><a href={{ url_for('profile.edit') }}>Profile Edit</a> (login required)</p>
{% endblock %}

When redirecting to profile.view

@bp.route('/view')
@login_required
def view():
    return 'This is the profile view'

It hits the @login_required decorator and redirects back to the login.

I'm using Flask-login (duh), and Flask-Pymongo for my database.

Why does the flask-login current user disappear on page refresh/redirect? Thanks!


Solution

  • My @login_manager.user_loader method was incorrect.

    Changed:

    @login_manager.user_loader
    def load_user(user_id):
        from flaskr.models import Guest
        guest = mongo.db.guests.find_one({'_id': user_id})
        if guest is None:
            return None
        return Guest(**guest)
    

    To:

    @login_manager.user_loader
    def load_user(user_id):
        from flaskr.models import Guest
        from flask_pymongo import ObjectId
        guest = mongo.db.guests.find_one({'_id': ObjectId(user_id)})
        if guest is None:
            return None
        return Guest(**guest)
    

    The database search was always failing because the user_id wasn't a Flask_Pymongo.ObjectId.