Login_required decorator
📖 Scenario: You are building a simple Flask web app that has some pages only accessible to logged-in users.To protect these pages, you want to create a login_required decorator that checks if a user is logged in before allowing access.
🎯 Goal: Create a login_required decorator in Flask that redirects users to the login page if they are not logged in.Use this decorator on a protected route to ensure only logged-in users can see it.
📋 What You'll Learn
Create a Flask app with a route
/dashboard that requires loginCreate a
login_required decorator that checks if session['logged_in'] is trueIf not logged in, redirect to
/loginUse the decorator on the
/dashboard route💡 Why This Matters
🌍 Real World
Web apps often need to restrict access to certain pages only to logged-in users. This decorator pattern helps keep code clean and reusable.
💼 Career
Understanding how to protect routes and manage user sessions is a key skill for backend web developers working with Flask or similar frameworks.
Progress0 / 4 steps