skip to Main Content

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



  1. 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.

    from flask_login import login_required, logout_user
    
    @app.route("/auth/logout")
    @login_required
    def logout():
        logout_user()
        return redirect(url_for(<your_homepage>))
    

    You simply need to put the following link to your navbar or on the profile section etc..

    ...
    <a href="url_for('logout')">Logout</a>
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search