Complete the code to import the redirect function from Remix.
import { [1] } from "@remix-run/node";
useLoaderData instead of redirect.The redirect function is imported from @remix-run/node to redirect unauthenticated users.
Complete the loader function to check if the user is authenticated.
export const loader = async ({ request }) => {
const user = await getUserFromRequest([1]);
if (!user) {
throw redirect('/login');
}
return json({ user });
};response instead of request.params or context.The request object contains information about the HTTP request and is used to get the user session.
Fix the error in the loader to correctly redirect unauthenticated users.
import { redirect } from "@remix-run/node"; export const loader = async ({ request }) => { const user = await getUserFromRequest(request); if (!user) { [1]('/login'); } return json({ user }); };
redirect() instead of throwing it.In Remix loaders, to redirect, you must throw the redirect() call, not just return it.
Fill both blanks to use the loader data and render the user's name.
import { useLoaderData } from "@remix-run/react"; export default function Profile() { const data = useLoaderData(); return <div>Welcome, [1]. Your email is [2].</div>; }
user.name directly without data.name and email fields.The useLoaderData() hook returns the data object from the loader, so you access data.user.name and data.user.email.
Fill all three blanks to create a protected route with loader and render user info.
import { json, [1] } from "@remix-run/node"; import { useLoaderData } from "@remix-run/react"; export const loader = async ({ request }) => { const user = await getUserFromRequest(request); if (!user) { [2]('/login'); } return json({ user }); }; export default function Dashboard() { const { user } = useLoaderData(); return <main>Hello, [3]!</main>; }
Import redirect to redirect unauthenticated users. In the loader, throw redirect('/login') is used to redirect. In the component, display the user's name with user.name.
