AI summary by TwinMind

Authentication: It’s Easier Than You Think

Theo - t3․gg

21m 5s

Core Auth Concepts

  • Auth = authentication + authorization, not packages/contexts/user data
  • Authentication: who is this person and are they actually them
  • Authorization: can they do this thing or not
  • Auth happens on server, ideally on request (not client-side)

Common Misconceptions

  • People ask about auth in React apps when auth should be server-side
  • Auth is not about packages, state, or user objects
  • Client-side auth determination is fundamentally flawed approach

Server-Side Auth Flow

  • Server validates user identity and permissions on each request
  • Example: "ban user" button works only if server validates user identity + room ownership + ban permissions
  • Both authentication and authorization happen server-side

Client-Server Auth Integration

  • Clients make requests (convenient because requests can be authenticated/authorized)
  • To get auth on client: make request to server
  • Don't check local storage or context - make goddamn request
  • Endpoint examples: /api/current, /me, /api/current-user
  • Next.js: TypeScript file in api directory returns session from headers

Auth Token Implementation

  • Use secure cookies (not local storage)
  • Libraries: Next Auth, Remix Auth
  • Secure cookie advantages:
    • Sent on every request to same TLD
    • Not accessible from JavaScript
    • Prevents XSS attacks
  • Local storage problems:
    • Not synchronized to server
    • Not validated
    • Fully accessible by external JavaScript/extensions/viruses

Authentication Flow

  • User opens page first time
  • User signs in → fires request to server
  • Server returns secure cookie
  • Client stores secure cookie (inaccessible to JavaScript)
  • All subsequent requests include cookie automatically

Authorization Flow

  • User makes request for data
  • Server receives request and parses cookie
  • Server checks what user cookie belongs to
  • Server checks if user authorized for this action
  • Server returns auth errors if unauthorized, data if authorized

Client-Side Auth Patterns

  • Request user profile picture when needed
  • Request page access permissions
  • If request returns auth error → redirect to login
  • After login → redirect back to original page
  • Show different UI based on server response (join button vs request to join button)

Auth Provider Guidelines

  • Keep auth providers "incredibly dumb"
  • Only track: are you authed or not
  • Optional: basic session info (username, etc.)
  • Prefer fetching user data from server when possible
  • Auth confirms identity and permissions - not data retrieval

JWT Considerations

  • JWTs can complicate the model
  • Keep JWT payloads minimal
  • Avoid bloating requests with large JWT data
  • Request specific data when needed instead

Practical Implementation (Zapdos Demo)

  • Next.js getServerSideProps for server-side auth checks
  • useSession hook for client-side session management
  • Redirect to login if no session found
  • Session validation via database lookup using cookie metadata

Role-Based Authorization Example

  • Added role enum field (admin/user) to database
  • Created tRPC endpoint to fetch user role
  • Protected procedure ensures session exists
  • Client-side conditional rendering based on role
  • Admin-only UI components hidden for non-admin users
  • Use isLoading (not isFetching) for proper loading states

Key Principles

  • Auth is request processing, not client-side logic
  • Most "auth questions" are actually data request flow questions
  • Separate auth concerns from data fetching concerns
  • Server determines permissions, client renders accordingly

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome