Challenge - 5 Problems
Astro Cookie Session Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat happens when a cookie is missing in Astro session management?
Consider an Astro server component that reads a session cookie to identify a user. What will the component render if the cookie is not present in the request?
Astro
export async function get({ request }) { const cookie = request.headers.get('cookie'); const session = cookie?.includes('sessionId=') ? 'User logged in' : 'Guest user'; return new Response(session); }
Attempts:
2 left
💡 Hint
Think about how the code checks for the session cookie before deciding the user state.
✗ Incorrect
The code checks if the cookie header includes 'sessionId='. If the cookie is missing, the condition fails, so it returns 'Guest user'. No error is thrown because optional chaining safely handles missing cookies.
📝 Syntax
intermediateIdentify the syntax error in setting a cookie in Astro
Which option correctly sets a cookie named 'sessionId' with value 'abc123' in an Astro server response?
Astro
export async function get() { return new Response('OK', { headers: { 'Set-Cookie': 'sessionId=abc123; HttpOnly; Path=/' } }); }
Attempts:
2 left
💡 Hint
Headers keys must be strings and cookie attributes separated by semicolons.
✗ Incorrect
Option A correctly uses a string key 'Set-Cookie' and a properly formatted cookie string with semicolons separating attributes. Option A misses quotes around the header key, causing syntax error. Option A uses invalid syntax inside the string. Option A misses semicolons between cookie attributes.
❓ state_output
advancedWhat is the session state after cookie expiration in Astro?
If an Astro app sets a cookie with 'Max-Age=0', what will be the session state on the next request?
Astro
export async function get({ request }) { const cookie = request.headers.get('cookie'); const sessionActive = cookie?.includes('sessionId=') ? true : false; return new Response(sessionActive ? 'Active session' : 'Session expired'); }
Attempts:
2 left
💡 Hint
Remember what 'Max-Age=0' means for cookies in browsers.
✗ Incorrect
'Max-Age=0' instructs the browser to delete the cookie immediately. Therefore, on the next request, the cookie is missing, so the session is expired.
🔧 Debug
advancedWhy does this Astro session cookie not persist?
Given this code snippet, why does the session cookie not persist across requests?
export async function get() {
return new Response('OK', {
headers: {
'Set-Cookie': 'sessionId=abc123; Path=/; Secure'
}
});
}
Astro
export async function get() { return new Response('OK', { headers: { 'Set-Cookie': 'sessionId=abc123; Path=/; Secure' } }); }
Attempts:
2 left
💡 Hint
Check the meaning of the 'Secure' attribute in cookies.
✗ Incorrect
The 'Secure' attribute means the cookie is only sent over HTTPS connections. If testing on HTTP, the cookie will not be stored or sent back, so it appears not to persist.
🧠 Conceptual
expertHow does cookie-based session management handle concurrent requests in Astro?
In Astro, when multiple concurrent requests from the same client arrive, how does cookie-based session management ensure session consistency?
Attempts:
2 left
💡 Hint
Think about where session state is stored and how cookies behave in browsers.
✗ Incorrect
Cookies are sent by the browser with each request independently. Astro reads the cookie per request. Session consistency depends on how the server manages session data, not on cookie locking or merging.
