Back to Prompts
Coding Assistant

Add a Simple Login Check

Start here - simplest middleware pattern From the Middleware Basics AI Coding Building Block.

Prompt
Create simple middleware that checks if a user is logged in before they can access protected pages.

Framework: [Express, Flask, Django, FastAPI, etc.]

Keep it simple:
1. Check if the user has a valid session or token
2. If yes: let them through to the page
3. If no: redirect to login page (or return 401 error for APIs)

I want to understand the basic pattern first. Show me:
- The middleware function itself (keep it short!)
- How to apply it to routes that need login
- How to skip it for public pages (like homepage, login page)

I'm learning, so explain each part simply.
1 views 0 copies