r/reactjs • u/WetThrust258 • May 26 '26
Needs Help Clean Architecture in React.js (Monorepo) with Auth Libraries + TanStack Query/RTK Query — how do you structure it realistically?
I’ve been trying to properly understand and implement Clean Architecture on the frontend with Next.js (frontend-focused, not backend), but I’m getting confused once third-party libraries enter the picture — especially auth libraries and query libraries.
Most examples online are either:
- too theoretical,
- too backend-oriented,
- or avoid real-world libraries entirely.
My setup is something like:
- React.js
- Monorepo (Turborepo)
- Shared packages
- Auth library (like Better Auth / NextAuth / Clerk / etc.)
- TanStack Query or RTK Query
- TypeScript
What I’m struggling with is:
1. Where should auth libraries live in Clean Architecture?
For example:
- should the auth client be wrapped behind interfaces/adapters?
- should the UI directly call the auth library hooks/functions?
- where do auth-related side effects belong?
- how much abstraction is “too much”?
Example:
authClient.signIn.email(...)
Should this be hidden behind:
AuthRepository.signIn()
or is that unnecessary abstraction on the frontend?
2. How do query libraries fit into Clean Architecture?
If using:
- TanStack Query
- RTK Query
then:
- where should queries/mutations live?
- do you create “use cases” on the frontend?
- should hooks directly call APIs?
- should query libraries exist only in the infrastructure layer?
I see people saying:
- “don’t leak library-specific code”
- “keep business logic isolated”
But in frontend apps, the query library becomes deeply integrated into state management and caching.
So what does a realistic architecture look like?
3. Monorepo structure confusion
How do you structure things in a monorepo without overengineering?
For example:
apps/
web/
mobile/
packages/
auth/
api-client/
ui/
contracts/
utils/
Do you:
- keep auth client inside a shared package?
- export inferred types from there?
- keep query logic centralized?
- create repositories/services/use-cases packages?
How far do people actually go in production apps?
4. How do you avoid overengineering?
This is probably my biggest issue.
I understand the theory of:
- entities
- repositories
- use cases
- dependency inversion
But when implementing real frontend apps, I end up wondering:
- am I just wrapping libraries for no reason?
- am I creating abstractions that add zero value?
- how do experienced frontend engineers decide what deserves abstraction?
I’d really appreciate:
- real-world folder structures,
- examples from production apps,
- monorepo-specific advice,
- or articles/repos/videos that show Clean Architecture with modern frontend tooling.
Especially interested in opinions from people using:
- React.js
- TanStack Query
- RTK Query
- Better Auth / NextAuth / Clerk
- Turborepo monorepos
9
u/Cahnis May 26 '26
Dont. Its a OOP backend model. You are on functional frontend land.
27
u/Risc12 May 26 '26
Just do not.
Clean Architecture aims to solve a set of problems that you most probably do not have.
Go the minimal way, and preferably “screaming architecture”.
You have no need for an adapter to tanstack query, that is already an abstraction.
You do not need an adapter for the UI, that is react.
You do not need to abstract away client/server communication, that is Nextjs.
Nextjs also has serious vendor lock-in, but that gives you expressiveness. If you give up the expressiveness you might as well drop Nextjs.
In typescript/javascript, tests are easier than the languages CA was designed for, because Typescript has structural typing, so you mostly do not need interface-adapters.
——
I mean it, aim to write as little as possible “glue”, just roughly separate by concerns or functionality (based on domain, not on tech).
1
u/namesandfaces Server components May 26 '26
Btw, I don't recommend BetterAuth or NextAuth; you can strike off NextAuth or Auth.js because both projects have been officially taken over by BetterAuth.
I don't recommend BetterAuth because I think it asks too much investment from the developer in terms of building a lot of BetterAuth-specific stuff.
4
u/doxara May 26 '26
Thank me later: https://fsd.how/
3
u/Miklet92 May 26 '26
I recommend you reading this article. It may help you get a bit different view what clear architecture could be in terms of the frontend: https://martinfowler.com/articles/modularizing-react-apps.html
3
u/Merry-Lane May 26 '26
1
u/locomain May 26 '26
There is one comment here mentioning hexagonal. I would probably do that in combination with domain driven design. It is pretty close to clean architecture. Separating the layers with ports and adapters keeps your app very clean and learns you to categorize stuff better. The experience will probably learn you when to make abstractions as well. Also, when there is not a lot going on inside the app, it might seem like it’s overengineering and wrapping. When the app gets bigger it’s a breath of fresh air.
0
u/Savalava May 26 '26
It sounds like you're overthinking this due to inexperience
RE
But when implementing real frontend apps, I end up wondering:
Yes, you probably are.
The only thing that matters is if your app is secure and passes a pen-test. Nothing else matters.
Think about it on the high level of token exchange. As long as your app is following best practices in terms of using JSON web tokens / HTTP only cookies, or whatever solution you're using, then the security is good. Nothing else matters.
Folder structure is irrelevant.
I have designed / implemented the auth layer on multiple enterprise projects. First few times I was constantly doubting myself, as you are now. Once my solutions started passing pen-tests I gained confidence and stopped doubting myself.
Hope that helps a little.
0
u/Leather-Historian722 May 26 '26
if you use tanstack query it plays best with tanstack start, look at the examples here : https://tanstack.com/start/latest/docs/framework/react/getting-started
0
u/fedekun May 26 '26 edited May 26 '26
myPost.create(...)) or use cases (myUseCase.execute(...)).The problem with models is that you can't really use backend models in the FE, so use cases are better for React-land. You can create a generic
executefunction which will use Tanstack Query to hit the backend and run an use case. Or you can have one function per use case too, up to you. Depending if you use RSC or not.3) This will depend but normally something like:
I recommend starting out with hexagonal architecture is a bit simpler and gives you basically the same advantages. The important part is your domain doesn't import from outside the
domainfolder. It has no dependencies, can be tested in isolation, etc.4) It is a "over-engineered" solution, works well for big teams or experienced teams already using similar methodologies. It has a bunch of advantages, like being able to not depend on a database to do work, very fast unit testing, etc.
I have a blog post about it I'll have to release some time :P
-1
u/Mean_Bicycle4447 May 26 '26
Hello, I'm building this, still not in alpha version but getting there this days: https://github.com/seba-cod3/saas-template
Here are some answers
Notes: NextJS has a mental model that aims to render on backend and hidrate on frontend, this works great for displaying data, like in a e-commerce, but fails a little on a really dynamic app. Some of the SaaS-Template aims to clear that up, check the repo, the idea is to build an npx-cli thingy-thing with it but right now the blocks are all on the table.