Complete the code to create a new session using Remix's session storage.
import { createCookieSessionStorage } from '@remix-run/node'; const sessionStorage = createCookieSessionStorage({ cookie: { name: '[1]', secrets: ['s3cret'], secure: true } });
The cookie name for the session is commonly set as 'session' to identify the session cookie.
Complete the code to get the session from the request headers.
export async function loader({ request }) {
const cookieHeader = request.headers.get('Cookie');
const session = await sessionStorage.[1](cookieHeader);
return new Response('Session loaded');
}The method to retrieve a session from the cookie header is getSession.
Fix the error in the code to destroy a session by committing an empty session.
export async function action({ request }) {
const session = await sessionStorage.getSession(request.headers.get('Cookie'));
return new Response('Logged out', {
headers: {
'Set-Cookie': await sessionStorage.[1](session)
}
});
}The correct method to destroy a session cookie is destroySession.
Fill both blanks to create a session and set a value before committing it.
export async function action({ request }) {
const session = await sessionStorage.[1](request.headers.get('Cookie'));
session.[2]('userId', '1234');
return new Response('Session set', {
headers: {
'Set-Cookie': await sessionStorage.commitSession(session)
}
});
}First, get the session with getSession. Then set a value with set.
Fill all three blanks to create a session, set a user name, and commit the session.
export async function action({ request }) {
const session = await sessionStorage.[1](request.headers.get('Cookie'));
session.[2]('username', 'alice');
return new Response('User logged in', {
headers: {
'Set-Cookie': await sessionStorage.[3](session)
}
});
}Use getSession to get the session, set to add data, and commitSession to save it.
