Flask-Login still logged in after use logouts when using remember_me

2024/9/19 9:26:44

To logout a user in flask using Flask-login, i simply call logout_user(), but after adding some additional checks with session, after I click logout and click back to "login page" again, i'm still logged in. It happens only when I choose to "remember me"

I think I misunderstand the concept of session and logout_user() here, can anyone please clarify and help?

In my opinion, i think when I clear the session then everything inside including 'user_id', 'username' etc... will also be cleared. But somehow the 'user_id' or 'username' field still exists. I think that's what causes the problem.

My Code is below:

Logout part:

@mod.route('/logout/')
@login_required
def logout():logout_user()session.clear()return redirect(url_for('users.login'))

Login Part:

@mod.route('/login/', methods=['GET', 'POST'])
def login():if g.user is not None and g.user.is_authenticated():return redirect(url_for('users.home'))form = LoginForm(request.form)if form.validate_on_submit():user = User.query.filter_by(email=form.email.data).first()if user and check_password_hash(user.password, form.password.data):session['user_id'] = user.idsession['username'] = user.nicknamesession['remember_me'] = form.remember_me.dataremember_me = Falseif 'remember_me' in session:remember_me = session['remember_me']session.pop('remember_me', None)login_user(user, remember_me)return redirect(url_for('users.home'))return render_template("users/login.html", form=form)
Answer

For anyone still stumbling on this... The solution is to clear the remember-me cookie.

Here's an example:

@app.route('/logout', methods=['GET'])
def logout():flask_login.logout_user()session.clear()if session.get('was_once_logged_in'):del session['was_once_logged_in']# Delete rememberme cookie because logout_user does not do it for you.response = make_response(redirect(url_for('login')))response.delete_cookie('remember_token')return response

This is for when you're using the default cookie name for the remember-me cookie (remember_token.) Adapt the code with your own value if you customized the REMEMBER_COOKIE_NAME setting.

My friendly advice, though, would be to just avoid Flask altogether.

https://en.xdnf.cn/q/72548.html

Related Q&A

How to write integers to a file

I need to write ranks[a], ranks[b], countto a file, each time on a new lineI am using:file = open("matrix.txt", "w") for (a, b), count in counts.iteritems():file.write(ranks[a], ran…

seaborn changing xticks from float to int

I am plotting a graph with seaborn as sns and pylab as plt:plt.figure(figsize=(10,10),) sns.barplot(y = whatever_y, x = whatever_x , data=mydata) plt.xticks(fontsize=14, fontweight=bold)The xticks are …

What are the use cases for a Python distribution?

Im developing a distribution for the Python package Im writing so I can post it on PyPI. Its my first time working with distutils, setuptools, distribute, pip, setup.py and all that and Im struggling a…

Recovering a file deleted with python

So, I deleted a file using python. I cant find it in my recycling bin. Is there a way I can undo it or something. Thanks in advance.EDIT: I used os.remove. I have tried Recuva, but it doesnt seem to fi…

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

This question is related to a previous question I asked. Namely this one if anyone is interested. Basically, what I want to do is to expose a C array to Python using a Py_buffer wrapped in a memoryview…

selenium remotewebdriver with python - performance logging?

Im trying to get back some performance log info from a remote webdriver instance. Im using the Python Selenium bindings.From what I can see, this is information I should be able to get back. Think it m…

Python - replace unicode emojis with ASCII characters

I have an issue with one of my current weekend projects. I am writing a Python script that fetches some data from different sources and then spits everything out to an esc-pos printer. As you might ima…

How do I get my python object back from a QVariant in PyQt4?

I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView.My index() and parent() function creates the QModelIndex using the QAbstractItemModel inherited function createIndex and p…

Django serializers vs rest_framework serializers

What is the difference between Django serializers vs rest_framework serializers? I making a webapp, where I want the API to be part of the primary app created by the project. Not creating a separate A…

Pandas replace non-zero values

I know I can replace all nan values with df.fillna(0) and replace a single value with df.replace(-,1), but how can I replace all non-zero values with a single value?