Challenge - 5 Problems
Session Mastery in Remix
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat happens when you call
destroySession in Remix?Consider a Remix loader that calls
destroySession on the user's session cookie. What is the expected behavior in the browser after this response?Remix
import { destroySession, getSession } from '~/sessions'; export async function loader({ request }) { const session = await getSession(request.headers.get('Cookie')); return new Response(null, { headers: { 'Set-Cookie': await destroySession(session) } }); }
Attempts:
2 left
💡 Hint
Think about what destroying a session means for the cookie stored in the browser.
✗ Incorrect
Calling
destroySession returns a response that clears the session cookie in the browser, effectively logging the user out.❓ state_output
intermediateWhat is the session state after calling
commitSession with no changes?If you retrieve a session with
getSession and then immediately call commitSession without modifying it, what will the response cookie contain?Remix
import { getSession, commitSession } from '~/sessions'; export async function loader({ request }) { const session = await getSession(request.headers.get('Cookie')); return new Response('OK', { headers: { 'Set-Cookie': await commitSession(session) } }); }
Attempts:
2 left
💡 Hint
Think about whether commitSession changes the cookie if the session data is unchanged.
✗ Incorrect
If the session data is unchanged, commitSession returns the same cookie value, so the browser keeps the session as is.
📝 Syntax
advancedWhich option correctly destroys a session in an action function?
You want to destroy the user's session after a form submission in a Remix action. Which code snippet correctly does this?
Remix
import { destroySession, getSession } from '~/sessions'; export async function action({ request }) { const session = await getSession(request.headers.get('Cookie')); // Destroy session here return new Response('Logged out', { headers: { 'Set-Cookie': await destroySession(session) } }); }
Attempts:
2 left
💡 Hint
destroySession expects a session object, not a cookie string.
✗ Incorrect
destroySession requires the session object, not the raw cookie string. commitSession with maxAge 0 does not guarantee cookie removal.
🔧 Debug
advancedWhy does this session destroy code not clear the cookie?
This Remix loader tries to destroy the session but the cookie remains in the browser. What is the bug?
Remix
import { destroySession, getSession } from '~/sessions'; export async function loader({ request }) { const cookie = request.headers.get('Cookie'); const session = await getSession(cookie); const setCookie = await destroySession(session); return new Response('Done', { headers: { 'Set-Cookie': setCookie } }); }
Attempts:
2 left
💡 Hint
Check if the cookie header is updated in the response.
✗ Incorrect
destroySession returns a cookie header string that must be set in the response headers to clear the cookie.
🧠 Conceptual
expertWhat is the main difference between
commitSession and destroySession in Remix?Choose the best description of how
commitSession and destroySession differ in managing user sessions.Attempts:
2 left
💡 Hint
Think about what happens to the cookie in each case.
✗ Incorrect
commitSession writes the current session data to the cookie, while destroySession removes the cookie to end the session.