09 / 09

Implement withAuthentication HOC

javascript
Difficulty: 5/10
Topics: higher-order components, authentication flow, React context

Scenario Questions

0-2 years experience
  1. 1

    We have a simple login page and a Dashboard component. How would you wrap Dashboard with a withAuthentication HOC so that only logged‑in users can see it?

  2. 2

    If the authentication check inside the HOC is asynchronous, what would you render while waiting for the result?

2-5 years experience
  1. 1

    You added withAuthentication to several routes, but after a recent refactor the app crashes when a component receives undefined props. What could be wrong in your HOC implementation?

  2. 2

    Explain how you would modify the HOC to support both client‑side redirects and server‑side redirects in a Next.js app.

5-8 years experience
  1. 1

    Our app now has dozens of protected components and the HOC adds noticeable render latency. How would you optimise the withAuthentication HOC for performance at scale?

  2. 2

    Describe how you would design the HOC to work with multiple auth providers (e.g., JWT, OAuth) without tightly coupling to a specific auth library.

8+ years experience
  1. 1

    We are migrating from a class‑based codebase using HOCs to a function‑component codebase using hooks. How would you plan the transition for authentication while keeping backward compatibility?

  2. 2

    At the organization level, what guidelines would you establish for using withAuthentication versus other patterns (e.g., route guards, context providers) to ensure consistency across teams?

Follow-up Questions

  • How would you test this HOC?
  • What are the trade‑offs of using a HOC versus a hook for auth?
  • How does this affect server‑side rendering?