Building an Authentication Layer: Build From Scratch or Use Better Auth?

●1 ●1 ●6
calendar_today ago • schedule3 min read

I've been working on an authentication capability for a developer infrastructure project called Plughub, and the process has raised some interesting architectural questions.

The initial goal was simple: provide authentication as a reusable backend package that developers can integrate into their existing applications.

However, the more I explored the problem, the more I realised that authentication introduces architectural responsibilities that aren't as straightforward as they might initially appear.

Why Auth is different

Consider a typical email integration.

You configure a provider, send a message and handle the result.

Authentication is much more involved.

It needs to maintain a consistent relationship between users, credentials, external identities and sessions.

It also needs to account for:

  • Password hashing and credential verification.
  • OAuth state validation and secure callback handling.
  • Session creation, expiration and revocation.
  • Account linking and identity ownership.
  • Email verification and password recovery.
  • Concurrent requests and token consumption.
  • Security boundaries between cookies and bearer tokens.

These responsibilities are tightly connected.

A poorly designed account-linking flow, for example, can expose an account to someone who hasn't actually demonstrated ownership of its existing identity.

This is why I'm reluctant to approach authentication as a collection of unrelated endpoints.

The Better Auth question

One of the options I'm evaluating is Better Auth.

Rather than implementing every authentication mechanism ourselves, we could use Better Auth as an internal engine while Plughub defines its own integration experience and public contracts.

That sounds attractive, but I don't think adopting an established library automatically gives us a good architecture.

There are questions worth answering first:

  1. Are we building a genuine abstraction or simply renaming Better Auth APIs?
  2. How much of Better Auth's configuration and data model would leak into our public API?
  3. Would developers still have meaningful control over their database?
  4. What happens if we eventually need to replace the underlying engine?
  5. Are we introducing abstractions that solve real problems or just adding another layer?

The last two questions are particularly important.

Authentication data is not trivial to migrate. Users, credentials, sessions, provider identities and token formats can all be tied to the implementation.

An abstraction doesn't magically make those things interchangeable.

The integration problem

There's another architectural constraint I'm trying to preserve.

Plughub should not dictate how developers structure their applications.

A developer using Fastify shouldn't need to adopt a different application architecture just to use our authentication package.

The same should apply to someone using Express, Hono or another supported environment.

I'm therefore treating these as separate concerns:

Runtime package: owns the authentication functionality.

Application configuration: belongs to the developer's application.

Framework integration: connects the runtime to the application's HTTP environment.

Persistence: defines how authentication data is stored, without unnecessarily taking database ownership away from the developer.

CLI: provides optional installation and integration assistance.

This also means Backkit, our CLI, shouldn't be responsible for generating the actual authentication engine into a developer's repository.

The installed package should remain the source of runtime functionality.

Where I'm currently leaning

I'm interested in a composition approach.

Plughub should own its public contracts, integration philosophy and developer experience, while established implementations can be evaluated as internal dependencies.

But I'm not treating Better Auth as an automatic choice.

It needs to satisfy the architecture rather than the architecture being reshaped around it.

I think this is an interesting problem because it touches on something broader than authentication:

How do we build reusable developer infrastructure without turning it into another framework that developers must conform to?

I'd be interested in hearing from engineers who have built authentication systems or worked with Better Auth.

What trade-offs did you encounter when deciding whether to build your own engine or adopt an existing one?

Want more #opensource & #software-engineering deep dives?

Join 5,981+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

Stop Implementing Authentication Inside Containers on Kubernetes

Alexandre Vazquez - Jul 25

Local-First: The Browser as the Vault

Pocket Portfolio - Apr 20

Merancang Backend Bisnis ISP: API Pelanggan, Paket Internet, Invoice, dan Tiket Support

Masbadar - Mar 13

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4
chevron_left
155 Points • 8 Badges
2Posts
1Comments
2Connections
Just code and consistency.

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!