r/reactjs • u/Neat_Living_6765 • 22d ago
Discussion How do you usually secure your Next.js web apps?
Hey everyone!
To my knowledge, storing access tokens in localStorage can be risky because of XSS attacks
Yesterday, I came across this document: How to implement authentication in Next.js.
I decided to give it a try. Here's what I've come up with so far
- Use OpenSSL to generate an RSA key pair for verifying access tokens. The private key stays on the backend, while the public key is exposed to the frontend.
- Authentication: When a user successfully logs in, the backend sets the access token in cookie.
- In frontend, use Jose lib to fetch public key, and verify access token
- Implemented a Data Access Layer (DAL) with two functions:
- verifySession to verify the accessToken and redirect users to /login if the token is missing or invalid.
- checkRoles to check user roles to make sure users can't access pages they're not supposed to.
The doc also suggests adding a DTO layer, but I skipped it since I'm using OpenAPI specs and TypeScript type generation.
I'd really appreciate it if you could share your thoughts:
Do you see any potential security issues or problems with this flow?
Is there anything you would approach differently?
9
u/yksvaan 22d ago
Separate backend that handles all auth, business logic and in general everything not public.
Tokens should only be in httponly cookies unless there's a very good reason to do otherwise.
If you need to have backend for frontend, it should only be limited to doing some basic token validity checks with the public key and other sanity checks.
4
u/amayle1 21d ago
Why would you need to verify the signature of the Jwt on the front end? That’s not even secure, somebody could just strip that JavaScript out.
You sign the JWT with your private key when you issue it. Your backend then verifies the signature using your public key upon receiving requests.
2
u/danielraison 19d ago
Don't fetch the public key on every request. Expose it as a JWKS (/.well-known/jwks.json) and use jose's createRemoteJWKSet. It caches the key and picks the right one by kid, so you can rotate keys later without redeploying the frontend.
Check more than the signature. Pass algorithms: ['RS256'], issuer and audience to jwtVerify. Pinning the algorithm closes off the classic algorithm-confusion tricks, and issuer/audience stop a token issued for another service from working on yours.
Signature-only checks can’t revoke anything. If you disable a user or change their role, their current token stays valid until it expires, roles included. Keep the access token short (5 to 15 minutes) and put the refresh token in a separate httpOnly cookie, ideally with a Path limited to the refresh endpoint so it isn’t sent with every request.
Server Actions are public POST endpoints. Checking the page doesn't protect the actions it uses. Call verifySession/checkRoles inside each action, the same way you would in a route handler.
Don't make middleware your only check. CVE-2025-29927 let anyone skip Next.js middleware entirely with a single header. It's patched now, but it's a good reminder of why the DAL approach in the docs is the right one.
6
u/CesnaitisSwitlana 16d ago
If your cookie is httpOnly like it should be, your frontend cant even read the token to verify it with Jose. do the token check inside your server components or middleware instead of the client. I use npx slices for my auth setup now so I dont mess up the plumbing, but regardless never trust client side role checks anyway.
1
u/Alarming_Ad_257 21d ago
All good but just one thing if the cookie is httpOnly, frontend JS cant read it, so step 3 doesnt really work. And if it’s not httpOnly, you’ve reopened the XSS problem you were trying to avoid.
2
u/maksymkoval 21d ago
Looks solid overall. Two things I'd check:
1
u/Old_Elderberry1392 21d ago
Drop the client-side verification step entirely. An httpOnly cookie is unreadable by JavaScript so that code path is physically impossible to execute unless you intentionally remove the security flag and reintroduce XSS risks. Keep token validation strictly in server components or middleware where the request headers are accessible before rendering
1
u/AlexDjangoX 17d ago
Clerk is your best friend
2
u/[deleted] 22d ago
[removed] — view removed comment
0
u/[deleted] 21d ago
[removed] — view removed comment