import { redirect } from '@remix-run/node'; export async function loader({ request }) { const user = await getUserFromSession(request); if (!user) { return redirect('/login'); } return { user }; }
The redirect function sends the user to the specified URL immediately. If the user is not authenticated, they are sent to /login.
export async function loader({ request }) { const user = await getUserFromSession(request); if (!user) { return redirect('/login'); } return { user: { id: user.id, name: user.name } }; }
The loader returns an object with a user property containing the user's id and name. This data is passed to the component as props.
Option C correctly awaits the async call and returns redirect to send the user away. Option C misses return before redirect, so it won't stop execution. Option C misses async/await. Option C returns a string instead of redirect.
export async function loader({ request }) { const user = await getUserFromSession(request); if (!user) { return redirect('/protected'); } return { user }; }
The loader redirects unauthenticated users to the same protected route, causing the loader to run again and redirect again, creating a loop.
Loaders in parent routes run before child loaders. Adding authentication in the parent loader protects all nested routes efficiently. Doing it in every child is repetitive. Root loader may be too high-level. Client-side checks alone are insecure.
