What if one missed check lets strangers see your private data? Middleware stops that risk effortlessly.
Why Protected routes and middleware in Astro? - Purpose & Use Cases
Imagine building a website where some pages should only be seen by logged-in users, like a personal dashboard or settings page. You try to check user access on every page manually by adding code everywhere.
Manually checking user access on every page is tiring and easy to forget. If you miss one page, anyone can see private info. It also makes your code messy and hard to update.
Protected routes and middleware let you centralize access checks. Middleware runs before your pages load, blocking unauthorized users automatically. This keeps your code clean and secure.
if (!user.isLoggedIn) { redirect('/login') } // repeated in every page
middleware(() => { if (!user.isLoggedIn) redirect('/login') }) // one place for all routesYou can easily protect many pages with one simple setup, ensuring only the right users see sensitive content.
Think of a gym where only members can enter certain rooms. Middleware is like a security guard checking membership at the door, so you don't need a guard in every room.
Manual checks on every page are error-prone and messy.
Middleware centralizes access control for protected routes.
This keeps your site secure and your code clean.
Practice
Solution
Step 1: Understand middleware role
Middleware runs before a page loads to perform checks or actions.Step 2: Identify middleware use in protected routes
It checks if a user has permission to access certain pages before showing them.Final Answer:
To check user access before loading a page -> Option AQuick Check:
Middleware checks access = D [OK]
- Thinking middleware styles pages
- Assuming middleware fetches data after load
- Confusing middleware with image optimization
/dashboard in Astro?Solution
Step 1: Recall matcher syntax
Matcher must be an array of strings specifying route patterns.Step 2: Match all routes under /dashboard
Using '/dashboard/*' inside an array matches all subpaths.Final Answer:
export const matcher = ['/dashboard/*']; -> Option BQuick Check:
Matcher is array with pattern string = A [OK]
- Using string instead of array for matcher
- Missing wildcard * to match subroutes
- Using exact path without wildcard for nested routes
/profile without a valid token?export const matcher = ['/profile'];
export async function middleware({ request, redirect }) {
const token = request.headers.get('authorization');
if (!token) {
return redirect('/login');
}
return;
}Solution
Step 1: Check token presence
The middleware checks if the 'authorization' header exists in the request.Step 2: Redirect if no token
If token is missing, middleware redirects user to '/login'.Final Answer:
User is redirected to /login -> Option CQuick Check:
No token means redirect to /login = A [OK]
- Assuming user sees page without token
- Expecting error instead of redirect
- Redirecting to wrong path
/admin routes:export const matcher = '/admin/*';
export function middleware({ request, redirect }) {
const user = request.headers.get('user');
if (user !== 'admin') {
redirect('/not-authorized');
}
}Solution
Step 1: Check matcher syntax
Matcher must be an array of strings, not a single string.Step 2: Confirm redirect usage
Redirect can be called synchronously; no await needed here.Final Answer:
matcher should be an array, not a string -> Option DQuick Check:
Matcher array required = C [OK]
- Using string instead of array for matcher
- Thinking redirect needs await
- Confusing request.headers API
/dashboard and /settings routes using middleware in Astro. Which matcher setup correctly protects both paths and their subpaths?Solution
Step 1: Understand wildcard usage
'/*' matches all subpaths under a route, ensuring protection of nested pages.Step 2: Verify matcher array format
Matcher must be an array of strings with correct wildcard patterns.Final Answer:
export const matcher = ['/dashboard/*', '/settings/*']; -> Option AQuick Check:
Array with '/*' wildcard protects subpaths = B [OK]
- Omitting wildcard to protect subpaths
- Using incorrect wildcard like '**'
- Using string instead of array for matcher
Ready to prove it?
Pass the test and this pattern moves unknown
