I want to implement frontend to send logout request to the flask backend below,
from flask_login import login_required, logout_user
@app.route('/auth/logout', methods=['POST'])
@login_required
def logout():
logout_user()
return "success", 200
I only know I should include the credential information inside the POST data.
However, even I read through the document:
https://flask-login.readthedocs.io/en/latest/
and google on the internet, I didn’t find a working example of frontend code that works with this kind of backend API
Is there anyone who can provide a simple frontend code example to call this backend api with credential included, thanks!
2
Answers
Just to start further discussion on this issue. I found several code example
Logout request from Javascript to Flask
https://stackoverflow.com/a/77105026/15233792
https://github.com/pallets-eco/flask-security/issues/684#issuecomment-334174094
No credential information is needed, you will not be making a post request. Login system works with cookies. Meaning when you delete the cookie from the browser, you are logged out.
Therefore it’s trivial to logout by visiting a link in the html. And then flask-login will delete the user from the session, logging you out.
You simply need to put the following link to your navbar or on the profile section etc..