@vivekcsein-logo
  • Home
  • About
  • Development

    • Web Development
    • Performance & Optimization
    • Backend & APIs
    • Authentication & Security

    Specialized

    • SEO & Ad Monetization
    • AR Experiences
    • Animation
    • Figma to Code
  • Articles
  • Projects
  • Marketplace
  • Contact
@vivekcsein-logo

A portfolio full stack developer with a passion for building scalable and high-performance web applications.

Explore

  • Home
  • About

Services

  • Web Development
  • Backend & APIs
  • Performance Optimization
  • Security & Authentication

Connect

  • Contact
  • Privacy
  • Terms
  • Home
  • About
  • Web Development
  • Backend & APIs
  • Performance Optimization
  • Security & Authentication
  • Contact
  • Privacy
  • Terms

© @vivekcsein. All rights reserved.

  • Home
  • All Articles47
  • Featured
  • Recently Added
  • Most Read
  • Bookmarks
Topics
  • Interview3
  • 3d4
  • Ai Prompts2
  • Business3
  • Deployment1
  • Dev2
  • Frontend5
  • Getting Started2
  • Security2
  • Seo2
  • Workflow10
  • Backend1
  • Deployment1
  • Security1
    • Build auth with Next.js
  • Javascript1
  • Client Advisory5
  • Engineering2
Collections
  • Docs
  • Tutorials
  • Resources
  • Articles
“A better developer every single day.”
— Vivek
  1. Tutorials
  2. Security
  3. Build auth with Next.js
Security

Build auth with Next.js

A from-scratch walkthrough of cookie-based auth in a Next.js App Router project — no third-party auth service.

Updated Sep 12, 20261 min read
On this page

On this page

  • 1. Sign a session token
  • 2. Verify it in middleware
  • 3. Read the session in Server Components

This walks through a minimal, cookie-based auth flow: a signed session cookie, a middleware check, and a login route — no external auth provider.

1. Sign a session token#

Use a small library (or the Web Crypto API directly) to sign a payload containing the user id and an expiry, and set it as an httpOnly, secure cookie on login.

2. Verify it in middleware#

middleware.ts reads the cookie on every request to a protected route, verifies the signature, and redirects to /login if it's missing or invalid.

3. Read the session in Server Components#

Re-verify the same cookie in a getSession() helper called from Server Components — middleware protects the route, but the page still needs the user's identity to render anything personalized.

Keep reading

  • Backend

    Create an API with Hono

    Standing up a typed REST API with Hono, from an empty project to a validated, deployable route.

    1 min read·Sep 14, 2026
  • Security

    The Complete Guide to Web Authentication

    A beginner-to-advanced guide to web authentication: XSS, CSRF, cookies, sessions, JWT, OAuth, MFA and RBAC, with full Hono, Express and Fastify implementations.

    41 min read·Sep 12, 2026
  • Security

    RBAC & Secure Client-Side Data Handling in Next.js

    Role-based access control and secure data handling in Next.js: four enforcement layers from middleware to client components, plus common pitfalls and a 2026 stack.

    7 min read·Sep 12, 2026

On this page

  • 1. Sign a session token
  • 2. Verify it in middleware
  • 3. Read the session in Server Components