Auth with a Hosted Provider (Clerk)

Use a hosted identity service for sign-in UI, user management and MFA, and understand the trade-offs of outsourcing auth.

Intermediate · about 4 hours · difficulty 2/5

Learn first: OAuth 2.0, OpenID Connect and Social Login, Server Actions and Mutations

Hosted providers like Clerk (also Auth0, Supabase Auth and Firebase Auth) handle sign-up, login, social providers, MFA and user management for you. You get polished UI components and fewer security responsibilities.

Integration

  • Wrap your Next.js app with the provider and add prebuilt SignIn, SignUp and UserButton components.
  • Use middleware to protect routes and server helpers to read the current user id.
  • Store your own app data (applications, profiles) in PostgreSQL keyed by the provider's user id.
  • Use webhooks to sync user creation and deletion into your database.

Trade-offs

  • Faster to ship, with MFA, bot protection and session management included.
  • Vendor lock-in and per-user pricing once you grow beyond the free tier.
  • User data lives with a third party, which matters for data protection obligations such as India's DPDP Act.

Pitfalls

  • Not verifying webhook signatures.
  • Duplicating user profile data in two places without a clear source of truth.
  • Assuming the provider does authorization for you; deciding what a user may do is still your job.

You are ready to move on when your portal uses a hosted provider for login, syncs users into PostgreSQL through a verified webhook, and enforces roles in your own code.

Key concepts

  • Prebuilt sign-in components
  • Middleware and server helpers
  • User sync webhooks
  • Organisations and roles
  • Pricing and lock-in
  • Supabase Auth or Firebase Auth

Other options for this step

  • Auth with Auth.js (NextAuth): Add sessions, OAuth providers and credentials login to a Next.js app with the open-source Auth.js library and your own database.

Mini project

Integrate Clerk into a copy of your portal, sync users into PostgreSQL via a signed webhook, and write a short comparison of setup time, cost at 10,000 users and data ownership versus Auth.js.

Check yourself

Your app stores applications keyed by user id, but users are managed by a hosted provider. How do you keep your database in sync when a user deletes their account?

Free resources