Complete the code to import the function that handles user sessions.
import { [1] } from '~/sessions.server';
The createUserSession function is used to start a user session after login.
Complete the code to redirect the user after successful login.
return redirect('/dashboard', { [1] });
HTTP status code 302 means a temporary redirect, commonly used after login.
Fix the error in the logout action to properly destroy the session.
export const action = async ({ request }) => {
return [1](request, '/login');
};The destroyUserSession function ends the user session and redirects to login.
Fill both blanks to check if a user is logged in and redirect accordingly.
export const loader = async ({ request }) => {
const user = await [1](request);
if (user) {
return [2]('/dashboard');
}
return null;
};getUserSession retrieves the current user session. If a user exists, redirect sends them to the dashboard.
Fill all three blanks to create a login action that validates credentials and creates a session.
export const action = async ({ request }) => {
const form = await request.formData();
const email = form.get('[1]');
const password = form.get('[2]');
const user = await verifyUser(email, password);
if (!user) {
return { error: 'Invalid credentials' };
}
return [3](user.id, '/dashboard');
};The form fields for login are usually 'email' and 'password'. After verifying, createUserSession starts the session and redirects.
