Consider a web app built with Astro. Why is it important to have server-side logic for authentication instead of relying only on client-side code?
Think about who controls the client-side code and what risks that poses.
Client-side code runs in the user's browser and can be changed or bypassed by the user. Server-side logic runs on a trusted server, so it can securely verify user credentials and protect sensitive data.
In an Astro app, if you only check user login on the client side, what is the likely outcome?
Think about what a user can do with client-side code they control.
Without server-side checks, users can manipulate client code to pretend they are logged in, bypassing security.
In Astro, at what point in the page lifecycle should server-side authentication logic run to protect sensitive pages?
Think about when the server can block unauthorized users before they see the page.
Server-side authentication should run during server-side rendering so unauthorized users never receive protected content.
Choose the Astro server-side code that properly checks if a user is authenticated before rendering a page.
Look for code that runs on the server and returns a 401 status if no user.
Option A runs server-side, checks user, and returns 401 Unauthorized if no user, protecting the page.
Given this Astro server-side code snippet, why might unauthorized users still see protected content?
export async function GET({ request }) {
const user = await checkUser(request);
if (!user) {
return { props: { user: null } };
}
return { props: { user } };
}export async function GET({ request }) { const user = await checkUser(request); if (!user) { return { props: { user: null } }; } return { props: { user } }; }
Think about what happens when user is null and how the server responds.
Returning props with user null still sends the page to the client. To block unauthorized users, the server should return a 401 status response instead.
