WhoBuilt

Authentication

Jul 3, 2020· 24 min· 23K views·Watch on YouTube

Learn Next.js presents a Mastering Next.js module on JWTs, Auth0, and Firebase authentication for the Day Drink application.

How JWTs carry identity

JWTs let two parties securely send information as a JSON object. A token has a header, payload, and signature, and is commonly sent in an Authorization header using the Bearer scheme. After login, the token can accompany requests to resources the user is allowed to access.

Comparing Auth0 and Firebase

Learn Next.js walks through a Next.js Auth0 example that uses API routes and a hook to fetch and store user information. The module uses Firebase for Day Drink, citing prior familiarity, its broader platform features, user management dashboard, and simpler setup for this example. Firebase creates signed ID tokens for users in a project.

Building a reusable Firebase auth hook

The Firebase integration is organized in a custom `useAuth` hook that exposes the current user and methods for signing in, signing up, and signing out. A context shares user state across the app, while an auth state listener updates that state and returns an unsubscribe function. The example uses email and password, with password reset mentioned as another possible method.

Handling sign-in forms and protected actions

The sign-in page combines `useAuth`, Chakra UI toast messages, and the Next.js router to show Firebase errors or redirect successful users to the deals page. React Hook Form manages required email and password fields, submission state, and validation errors. The form logic lives in a reusable component that can appear in a full-screen page or a modal when someone tries to vote or add a deal while logged out.

Choosing hosted or custom login screens

Learn Next.js closes by noting that Firebase and Auth0 can provide prebuilt or hosted login interfaces, reducing the amount of form code to maintain. Custom forms take more work but give the team control over branding and behavior. The Day Drink example uses custom flows built from Firebase, a hook, and reusable components.

Takeaways

  • Treat a JWT as a signed token with a header, claims payload, and signature, and send it with requests that need authentication.
  • Choose an auth provider based on project needs: the example favors Firebase for its simpler integration and broader platform features.
  • Keep user state and auth actions behind a reusable hook so pages and protected components can share the same flow.
  • Use form validation and clear error feedback for sign-in, and redirect only after authentication succeeds.
  • Decide whether prebuilt login screens or custom forms better fit the team's maintenance needs and branding.

Mentioned in this talk

Search inside this talk

What was claimed 8

Related talks