Consider this Astro middleware that checks for a valid authentication token before allowing access to a page.
export async function onRequest({ request, redirect }) {
const token = request.headers.get('Authorization');
if (!token || token !== 'valid-token') {
return redirect('/login');
}
return new Response('Access granted');
}What will the user see if they visit the protected page without a valid token?
export async function onRequest({ request, redirect }) { const token = request.headers.get('Authorization'); if (!token || token !== 'valid-token') { return redirect('/login'); } return new Response('Access granted'); }
Think about what the redirect function does when the token is missing or invalid.
The middleware checks the token and if it is missing or incorrect, it redirects the user to the login page. Only valid tokens allow access.
Review the following middleware code that should block unauthenticated users:
export async function onRequest({ request, redirect }) {
const token = request.headers.get('Authorization')
if (!token) {
redirect('/login')
}
return new Response('Welcome!')
}Which option correctly describes the syntax error?
export async function onRequest({ request, redirect }) { const token = request.headers.get('Authorization') if (!token) { redirect('/login') } return new Response('Welcome!') }
Consider what happens after calling redirect without returning it.
In Astro middleware, redirect returns a response that must be returned to stop further processing. Without return, the function continues and sends the welcome message.
Examine this middleware code:
export async function onRequest({ request, redirect }) {
const token = request.headers.get('Authorization');
if (token !== 'valid-token') {
redirect('/login');
}
return new Response('Access granted');
}Users without the valid token still see 'Access granted'. Why?
export async function onRequest({ request, redirect }) { const token = request.headers.get('Authorization'); if (token !== 'valid-token') { redirect('/login'); } return new Response('Access granted'); }
Check what happens after calling redirect without returning it.
Redirect returns a response that must be returned to stop the middleware. Without return, the function continues and sends the access message.
Given this middleware snippet:
export async function onRequest({ request, redirect }) {
const token = request.headers.get('Authorization');
if (!token) {
return redirect('/login');
}
return new Response('Welcome!');
}What HTTP status code will the client receive if redirected?
export async function onRequest({ request, redirect }) { const token = request.headers.get('Authorization'); if (!token) { return redirect('/login'); } return new Response('Welcome!'); }
Redirects usually use status codes in the 3xx range. Which one is common for temporary redirects?
Astro's redirect helper sends a 302 status code by default for temporary redirects.
In Astro, you can check authentication either inside each page component or centrally in middleware.
Which reason best explains why middleware is preferred for authentication?
Think about when middleware runs compared to page code.
Middleware runs before the page loads, so it can stop unauthorized users early, saving resources and improving security.
