Bird
Raised Fist0
Remixframework~20 mins

Protected routes with loaders in Remix - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Remix Protected Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a user is not authenticated in this loader?
Consider this Remix loader code protecting a route. What will the user see if they are not logged in?
Remix
import { redirect } from '@remix-run/node';

export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    return redirect('/login');
  }
  return { user };
}
AThe user is redirected to the /login page.
BThe loader returns null and the page shows no content.
CThe user sees an error message on the same page.
DThe loader throws an exception and the app crashes.
Attempts:
2 left
💡 Hint
Think about what the redirect function does in Remix loaders.
❓ state_output
intermediate
2:00remaining
What data does the component receive from this loader?
Given this loader, what will the component receive as props?
Remix
export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    return redirect('/login');
  }
  return { user: { id: user.id, name: user.name } };
}
AAn error is thrown
B{ user: null }
Cundefined
D{ user: { id: '123', name: 'Alice' } }
Attempts:
2 left
💡 Hint
Look at what the loader returns when the user exists.
📝 Syntax
advanced
2:00remaining
Which loader code correctly protects a route and fetches user data asynchronously?
Choose the correct loader code that redirects unauthenticated users and returns user data.
A
export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    redirect('/login');
  }
  return { user };
}
B
export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    return '/login';
  }
  return { user };
}
C
export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    return redirect('/login');
  }
  return { user };
}
D
export function loader({ request }) {
  const user = getUserFromSession(request);
  if (!user) {
    return redirect('/login');
  }
  return { user };
}
Attempts:
2 left
💡 Hint
Remember to await async calls and return redirect properly.
🔧 Debug
advanced
2:00remaining
Why does this loader cause an infinite redirect loop?
Analyze this loader code. Why might it cause the user to be stuck redirecting endlessly?
Remix
export async function loader({ request }) {
  const user = await getUserFromSession(request);
  if (!user) {
    return redirect('/protected');
  }
  return { user };
}
ABecause getUserFromSession throws an error causing reloads.
BBecause unauthenticated users are redirected back to the protected route itself.
CBecause redirect is missing a return statement.
DBecause the loader returns user data even when user is null.
Attempts:
2 left
💡 Hint
Check where unauthenticated users are sent.
🧠 Conceptual
expert
3:00remaining
How can you securely protect nested routes with loaders in Remix?
You have a parent route with nested child routes. How should you structure loaders to protect all nested routes efficiently?
AAdd the authentication check only in the parent route loader, so all children inherit protection.
BUse client-side checks in components instead of loaders for protection.
CAdd the authentication check only in the root route loader and skip children.
DAdd the authentication check in every child route loader separately.
Attempts:
2 left
💡 Hint
Think about how Remix loaders cascade in nested routes.

Practice

(1/5)
1. What is the main purpose of using a loader in a protected route in Remix?
easy
A. To check if the user is authorized before showing the page
B. To style the page with CSS
C. To fetch data after the page is rendered
D. To handle button clicks on the page

Solution

  1. Step 1: Understand the role of loaders in Remix

    Loaders run before the page renders and can check user permissions.
  2. Step 2: Identify the purpose of protected routes

    Protected routes restrict access to authorized users only, so loaders check authorization.
  3. Final Answer:

    To check if the user is authorized before showing the page -> Option A
  4. Quick Check:

    Loader checks authorization = To check if the user is authorized before showing the page [OK]
Hint: Loaders run before page render to check access [OK]
Common Mistakes:
  • Thinking loaders style pages
  • Assuming loaders run after rendering
  • Confusing loaders with event handlers
2. Which of the following is the correct way to redirect unauthorized users inside a Remix loader?
easy
A. navigate('/login')
B. return redirect('/login')
C. throw redirect('/login')
D. window.location.href = '/login'

Solution

  1. Step 1: Recall Remix loader redirect syntax

    In Remix loaders, you use throw redirect() to redirect users.
  2. Step 2: Compare options

    throw redirect('/login') uses throw redirect('/login'), which is the correct Remix pattern.
  3. Final Answer:

    throw redirect('/login') -> Option C
  4. Quick Check:

    Use throw redirect() in loaders = throw redirect('/login') [OK]
Hint: Use throw redirect() inside loaders for redirects [OK]
Common Mistakes:
  • Using return redirect() instead of throw
  • Using client-side navigation methods in loaders
  • Trying to set window.location in server code
3. Given this loader code in Remix, what happens if the user is not logged in?
import { redirect } from '@remix-run/node';

export async function loader({ request }) {
  const user = await getUser(request);
  if (!user) {
    throw redirect('/login');
  }
  return { user };
}
medium
A. The loader returns null and the page is blank
B. The user sees the protected page with user data
C. The page shows an error message but stays on the same URL
D. The user is redirected to the /login page

Solution

  1. Step 1: Analyze the loader logic

    If user is falsy (not logged in), the loader throws redirect('/login').
  2. Step 2: Understand Remix redirect behavior

    Throwing redirect sends the browser to the specified URL before rendering the page.
  3. Final Answer:

    The user is redirected to the /login page -> Option D
  4. Quick Check:

    Throw redirect sends user away = The user is redirected to the /login page [OK]
Hint: Throw redirect sends user to new page before render [OK]
Common Mistakes:
  • Thinking user sees protected page anyway
  • Assuming error message shows instead of redirect
  • Believing loader returns null means blank page
4. Identify the error in this Remix loader for a protected route:
import { redirect } from '@remix-run/node';

export async function loader({ request }) {
  const user = await getUser(request);
  if (!user) {
    return redirect('/login');
  }
  return { user };
}
medium
A. Missing await before getUser(request)
B. Using return redirect() instead of throw redirect() causes a runtime error
C. Loader should not return user data
D. redirect should be imported from '@remix-run/react'

Solution

  1. Step 1: Check redirect usage in loader

    In Remix loaders, you must throw redirect(), not return it.
  2. Step 2: Understand consequences of return redirect()

    Returning redirect() does not stop loader execution properly and causes errors.
  3. Final Answer:

    Using return redirect() instead of throw redirect() causes a runtime error -> Option B
  4. Quick Check:

    Throw redirect required in loaders = Using return redirect() instead of throw redirect() causes a runtime error [OK]
Hint: Always throw redirect() in loaders, never return it [OK]
Common Mistakes:
  • Returning redirect() instead of throwing
  • Importing redirect from wrong package
  • Forgetting async/await on getUser
5. You want to protect a route so only logged-in users can access it. You also want to fetch user profile data in the loader. Which is the best way to write the loader?
import { redirect } from '@remix-run/node';

export async function loader({ request }) {
  const user = await getUser(request);
  if (!user) {
    throw redirect('/login');
  }
  const profile = await getUserProfile(user.id);
  return { user, profile };
}
hard
A. This loader correctly protects the route and fetches profile data
B. You should fetch profile data before checking user to avoid errors
C. You must return redirect('/login') instead of throwing it
D. Loaders cannot fetch multiple data items; split into two loaders

Solution

  1. Step 1: Check authorization logic

    The loader checks if user exists, and throws redirect if not, protecting the route.
  2. Step 2: Confirm data fetching after authorization

    Fetching profile after confirming user is safe and logical.
  3. Step 3: Validate return structure

    Returning both user and profile data together is valid and useful for the page.
  4. Final Answer:

    This loader correctly protects the route and fetches profile data -> Option A
  5. Quick Check:

    Check user, throw redirect, then fetch data = This loader correctly protects the route and fetches profile data [OK]
Hint: Check user first, then fetch data, throw redirect if unauthorized [OK]
Common Mistakes:
  • Fetching data before checking user
  • Returning redirect instead of throwing
  • Splitting data fetching unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard