<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp" -->

---
title: Secure NestJS + Next.js API Proxy Starter: The Ultimate...
description: A full-stack starter template implementing secure cookie-based authentication between Next.js App Router and NestJS backend. Features include HTTP-only...
canonical: https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Secure NestJS + Next.js API Proxy Starter: The Ultimate Guide to Cookie-Based Auth | daily.dev
og:description: A full-stack starter template implementing secure cookie-based authentication between Next.js App Router and NestJS backend. Features include HTTP-only...
og:url: https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp
og:image: https://api.daily.dev/og/posts/wgkPfGhQp.png
og:image:alt: Secure NestJS + Next.js API Proxy Starter: The Ultimate Guide to Cookie-Based Auth
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Secure NestJS + Next.js API Proxy Starter: The Ultimate Guide to Cookie-Based Auth

**[Kiss Tamás](https://daily.dev/sources/ymexwsv7diulfl9mdfbgs)** · [@kisstamas](https://daily.dev/kisstamas) · 2 min read · 0 upvotes · 0 comments

## Summary

A full-stack starter template implementing secure cookie-based authentication between Next.js App Router and NestJS backend. Features include HTTP-only encrypted session cookies using JWE, automatic token refresh with race condition handling, multi-device session support with device tracking, CSRF protection, and streaming capabilities for file uploads and downloads. The architecture uses a Next.js API proxy route handler to intercept 401 errors and refresh tokens server-side, keeping access tokens away from client JavaScript. Built with modern tooling including Drizzle ORM, PostgreSQL, Zod validation, and Docker Compose for local development.

## Content

Hey devs! 👋

I just built a robust starter template that solves one of the most common headaches in full-stack development: securely connecting a Next.js App Router frontend with a NestJS backend.

If you've ever struggled with where to store JWTs (localStorage vs. cookies), how to handle token refreshing on the server-side, or how to proxy requests without exposing sensitive data, this project is for you.

🌟 The Core Solution: Universal API Proxy

At the heart of this project is a Next.js Route Handler (/api/(api-proxy)/[...path]) that acts as a secure gateway. It intercepts 401s, refreshes access tokens automatically using HTTP-only cookies, and retries requests—all without the client-side code ever knowing.

🛡️ Security First Architecture

* **Zero Client-Side Tokens**: Access and Refresh tokens are stored in encrypted, HTTP-only, Secure, SameSite cookies. They never touch the browser's JavaScript.
* **Session Encryption**: We use jose (JWE) with HS256 to encrypt the entire session payload inside the cookie.
* **CSRF Protection**: By using SameSite=Lax cookies and server-side proxying, we mitigate CSRF risks naturally.

🔄 Smart Proxy & Advanced Session Management

* **Multi-Device Support**: The backend supports multiple active sessions per user. You can be logged in on your phone, laptop, and tablet simultaneously without one kicking the other out.
* **Device Tracking**: Every session automatically captures and stores the user's IP Address and User-Agent, giving you full visibility into active sessions for security auditing.
* **Concurrent Token Rotation**: The backend implements a Grace Period mechanism. If multiple requests trigger a refresh simultaneously, the backend temporarily accepts the "old" refresh token to prevent race conditions and "Token Revoked" errors.
* **Streaming Support**: Full support for file uploads (multipart/form-data) and binary downloads through the proxy.

🏗️ The Tech Stack Deep Dive

**Frontend: Modern & Type-Safe**

* Next.js 16 (App Router): Leveraging React Server Components for performance and SEO.
* Shadcn-ui
* Zod: Strict schema validation for forms and API responses.

**Backend: Scalable & Robust**

* NestJS 11: Enterprise-grade architecture with modules, guards, and interceptors.
* Drizzle ORM: The new standard for TypeScript ORMs—lightweight, edge-ready, and fully type-safe.
* PostgreSQL: Reliable relational data storage.
* Passport.js & Argon2: Industry-standard authentication strategies and secure password hashing.

🐳 DevOps Ready

* Docker Compose: Includes a complete setup for the PostgreSQL database, making local development a breeze.

Check out the code and let me know what you think!

[[Link to Repository]](https://github.com/kisstamasj/nestjs-nextjs-api-proxy.git)

## Similar posts on daily.dev

- [Handling Auth in Next.js 16 with Server Actions and Middleware](https://daily.dev/posts/handling-auth-in-next-js-16-with-server-actions-and-middleware-isvz1eirv) · Auth0 · 11 upvotes · 0 comments
- [Minting Next.js Authentication Cookies · Embrace The Red](https://daily.dev/posts/minting-next-js-authentication-cookies-embrace-the-red-yw0jvsl0q) · Embrace The Red · 1 upvotes · 0 comments
- [What's the best way to do authentication in modern applications](https://daily.dev/posts/what-s-the-best-way-to-do-authentication-in-modern-applications-7iho9es5q) · Neciu Dan · 25 upvotes · 0 comments
- [Protecting Next.js Applications in the Era of Server Actions](https://daily.dev/posts/protecting-next-js-applications-in-the-era-of-server-actions-wqyz3uq9e) · JS Dev Space · 2 upvotes · 0 comments
- [JWT Authentication in React: Secure Routes, Context, and Token Handling](https://daily.dev/posts/jwt-authentication-in-react-secure-routes-context-and-token-handling-p4wzm8hta) · Syncfusion · 1 upvotes · 0 comments

---

Tags: [#security](https://daily.dev/tags/security), [#typescript](https://daily.dev/tags/typescript), [#authentication](https://daily.dev/tags/authentication), [#nextjs](https://daily.dev/tags/nextjs), [#nestjs](https://daily.dev/tags/nestjs)

[View this post on daily.dev](https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp","headline":"Secure NestJS + Next.js API Proxy Starter: The Ultimate Guide to Cookie-Based Auth","text":"A full-stack starter template implementing secure cookie-based authentication between Next.js App Router and NestJS backend. Features include HTTP-only encrypted session cookies using JWE, automatic token refresh with race condition handling, multi-device session support with device tracking, CSRF protection, and streaming capabilities for file uploads and downloads. The architecture uses a Next.js API proxy route handler to intercept 401 errors and refresh tokens server-side, keeping access tokens away from client JavaScript. Built with modern tooling including Drizzle ORM, PostgreSQL, Zod validation, and Docker Compose for local development.","url":"https://daily.dev/posts/secure-nestjs-next-js-api-proxy-starter-the-ultimate-guide-to-cookie-based-auth-wgkpfghqp","datePublished":"2025-11-20T14:27:21.862Z","dateModified":"2025-11-20T14:54:10.539Z","author":{"@type":"Person","name":"Kiss Tamás","url":"https://daily.dev/kisstamas","image":"https://avatars.githubusercontent.com/u/48266482?v=4","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":160}},"image":"https://media.daily.dev/image/upload/s--yC5sfljn--/f_auto/v1763648842/posts/wgkPfGhQp?_a=BAMAK+ZW0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/sources/ymexwsv7diulfl9mdfbgs","name":"Kiss Tamás"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kiss Tamás","item":"https://daily.dev/sources/ymexwsv7diulfl9mdfbgs"},{"@type":"ListItem","position":3,"name":"Secure NestJS + Next.js API Proxy Starter: The Ultimate Guide to Cookie-Based Auth"}]}
```

