Sessions help remember who a user is while they use your app. Creating sessions lets you save user info, and destroying sessions logs them out safely.
Creating and destroying sessions in Remix
import { createCookieSessionStorage } from '@remix-run/node'; const sessionStorage = createCookieSessionStorage({ cookie: { name: 'session', secrets: ['your-secret'], sameSite: 'lax', path: '/', httpOnly: true, secure: process.env.NODE_ENV === 'production', }, }); // To create a session const session = await sessionStorage.getSession(request.headers.get('Cookie')); session.set('key', 'value'); const cookieHeader = await sessionStorage.commitSession(session); // To destroy a session const destroyCookieHeader = await sessionStorage.destroySession(session);
Use createCookieSessionStorage to set up session storage with cookie options.
Use commitSession to save session changes and destroySession to clear the session.
const sessionStorage = createCookieSessionStorage({
cookie: {
name: 'user_session',
secrets: ['my-secret'],
path: '/',
httpOnly: true,
secure: false,
},
});const session = await sessionStorage.getSession(request.headers.get('Cookie')); session.set('username', 'alice'); const cookieHeader = await sessionStorage.commitSession(session);
const session = await sessionStorage.getSession(request.headers.get('Cookie')); const destroyCookieHeader = await sessionStorage.destroySession(session);
This example shows how to create a session when a user logs in by saving their username. It also shows how to check the session to greet the user and how to destroy the session to log out.
import { createCookieSessionStorage, redirect } from '@remix-run/node'; const sessionStorage = createCookieSessionStorage({ cookie: { name: 'my_session', secrets: ['super-secret-key'], sameSite: 'lax', path: '/', httpOnly: true, secure: false, }, }); export async function action({ request }) { const formData = await request.formData(); const username = formData.get('username'); if (username) { // Create a new session and set username const session = await sessionStorage.getSession(); session.set('username', username); return redirect('/dashboard', { headers: { 'Set-Cookie': await sessionStorage.commitSession(session), }, }); } // If no username, just redirect back return redirect('/login'); } export async function loader({ request }) { const session = await sessionStorage.getSession(request.headers.get('Cookie')); const username = session.get('username'); if (!username) { return redirect('/login'); } return new Response(`Welcome, ${username}!`); } export async function logout({ request }) { const session = await sessionStorage.getSession(request.headers.get('Cookie')); return redirect('/login', { headers: { 'Set-Cookie': await sessionStorage.destroySession(session), }, }); }
Always keep your session secrets safe and never share them publicly.
Use httpOnly cookies to prevent JavaScript from accessing session cookies, improving security.
Remember to set secure: true in production to send cookies only over HTTPS.
Sessions let you save user info between requests using cookies.
Create sessions with createCookieSessionStorage and save data with commitSession.
Destroy sessions with destroySession to log users out safely.
Practice
Solution
Step 1: Understand what sessions do
Sessions store user data so it can be accessed on multiple pages.Step 2: Identify the correct purpose
Remembering user data across pages is exactly what sessions do using cookies.Final Answer:
To remember user data across different pages using cookies -> Option DQuick Check:
Sessions = Remember user data [OK]
- Confusing sessions with styling or database tasks
- Thinking sessions optimize images
- Assuming sessions run migrations
Solution
Step 1: Recall Remix session creation syntax
Remix uses createCookieSessionStorage with a cookie config object.Step 2: Match the correct syntax
const sessionStorage = createCookieSessionStorage({ cookie: { name: 'session' } }); matches the exact Remix pattern for creating session storage.Final Answer:
const sessionStorage = createCookieSessionStorage({ cookie: { name: 'session' } }); -> Option AQuick Check:
Use createCookieSessionStorage with cookie config [OK]
- Using incorrect function names like createSessionStorage
- Trying to instantiate with new keyword
- Using cookieSessionStorage which doesn't exist
destroySession(session) in Remix?const session = await sessionStorage.getSession(request.headers.get('Cookie'));
const response = new Response('Logged out');
return redirect('/login', {
headers: {
'Set-Cookie': await sessionStorage.destroySession(session)
}
});Solution
Step 1: Understand destroySession behavior
destroySession removes the session cookie to log the user out.Step 2: Analyze the redirect with Set-Cookie header
The response sets the cookie header to destroy the session and redirects to /login.Final Answer:
It clears the session cookie and redirects the user to /login -> Option CQuick Check:
destroySession clears cookie + redirect = It clears the session cookie and redirects the user to /login [OK]
- Thinking destroySession takes no arguments
- Assuming session remains active after destroySession
- Confusing destroySession with creating a new session
const session = await sessionStorage.getSession(request.headers.get('Cookie'));
return redirect('/logout', {
headers: {
'Set-Cookie': sessionStorage.destroySession(session)
}
});Solution
Step 1: Check async function usage
destroySession returns a promise, so it must be awaited to get the cookie string.Step 2: Verify header usage and arguments
Header 'Set-Cookie' is correct and destroySession requires the session argument.Final Answer:
Missing await before sessionStorage.destroySession -> Option AQuick Check:
Async destroySession needs await [OK]
- Forgetting to await async session methods
- Using wrong header names for cookies
- Passing wrong arguments to destroySession
1. Get the current session from the request cookie.
2. Destroy the session to clear cookie data.
3. Redirect the user to the login page with the destroyed session cookie set.
4. Create a new session with empty data before redirecting.
Solution
Step 1: Understand session destruction process
Getting the current session is needed to destroy it properly.Step 2: Recognize that creating a new empty session is unnecessary
Destroying the session clears the cookie; no new session is needed for logout.Step 3: Confirm redirect with destroyed session cookie
Redirecting with the destroyed session cookie logs the user out securely.Final Answer:
Steps 1, 2, and 3 only; creating a new session is unnecessary -> Option BQuick Check:
Destroy session + redirect = logout [OK]
- Creating a new empty session after destroying
- Skipping getting the current session
- Not redirecting after destroying session
Ready to prove it?
Pass the test and this pattern moves unknown
