You’ve successfully authenticated, Nice. Now only logged in users can see our zesty recipe! But now that you’re done you don’t want to remain logged in. Let’s enable the logout feature to protect the recipe.

Flask-login provides us with a logout_user method to facilitate this.

@app.route("/logout") @login_required def logout(): logout_user() return redirect(url_for('index'))

Awesome! We have our logout() function set up to call flask-login‘s logout_user() function to log out the user. Then we redirect the user to go back to the index page. Let’s implement a logout link in our HTML to trigger the logout code to run.

in logged_in.html update the code with the logout link:

<!DOCTYPE> <head> </head> <body> <a href="{{ url_for('logout') }}">Logout</a> </body>

When a user clicks the logout link, they’ll call the logout() function we just created.

Congrats on learning authentication with Flask!



Currently, we’re importing many functions from flask_login. We need to import one more, logout_user.


The logic for logging out a user is already provided, so it’s time to create the logout link. First, navigate to the logged_in.html page.

Inside the HTML page, add an <a> element that contains the text Logout and an href attribute with the value {{ url_for('logout') }}.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?