Setting Up Auth.js in Next.js: Providers, Sessions, and Route Protection

We often see developers spending weeks implementing social login, only to run into hydration bugs or session leaks. NextAuth.js (now Auth.js) solves this out of the box in 1–4 days. In our practice — 30+ projects with various providers, from Google to custom. We share a ready-made solution that prot

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:

Frequently Asked Questions

Latest works

  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1283
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1238
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    981
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    1029
  • image_website-sbh_0.webp
    Website development for SBH Partners
    1104
  • image_website-_0.webp
    Website development for Red Pear
    552

We often see developers spending weeks implementing social login, only to run into hydration bugs or session leaks. NextAuth.js (now Auth.js) solves this out of the box in 1–4 days. In our practice — 30+ projects with various providers, from Google to custom. We share a ready-made solution that protects your application and saves budget.

Modern applications require reliable authentication: OAuth, email magic link, login/password. We integrate Auth.js v5 with App Router and Edge Runtime, ensuring fast rendering and security at all stages. Unlike custom solutions, Auth.js provides ready-made providers, automatic CSRF protection, and session management via JWT or database sessions. The choice of strategy depends on load and requirements — we help determine the optimal option. Outcome: time savings of 2–3 times and no security headaches.

For example, on one e‑commerce project we integrated Auth.js with Google OAuth and database sessions. The result: login time dropped from 2.5s to 0.3s due to edge rendering, and we eliminated CSRF vulnerabilities entirely. The client’s support team deployed the update within a day.

What problems does NextAuth.js solve?

NextAuth.js covers three main tasks: session security, multi-provider support, and hybrid SSR/CSR. Sessions can be JWT or database sessions with Prisma. Multi-provider: Google, GitHub, email magic link, login/password — all in one config. Hybrid SSR/CSR: session available both on server and client without extra requests. We configure each of these aspects for a specific project, considering load and security requirements.

How we configure Auth.js v5

We use Auth.js v5 with App Router and Edge Runtime. Configure in auth.ts:

// auth.ts import NextAuth from 'next-auth'; import Google from 'next-auth/providers/google'; import GitHub from 'next-auth/providers/github'; import Credentials from 'next-auth/providers/credentials'; import { PrismaAdapter } from '@auth/prisma-adapter'; import { prisma } from '@/lib/prisma'; export const { handlers, signIn, signOut, auth } = NextAuth({ adapter: PrismaAdapter(prisma), providers: [ Google({ clientId: process.env.AUTH_GOOGLE_ID!, clientSecret: process.env.AUTH_GOOGLE_SECRET! }), GitHub({ clientId: process.env.AUTH_GITHUB_ID!, clientSecret: process.env.AUTH_GITHUB_SECRET! }), Credentials({ credentials: { email: {}, password: {} }, async authorize(credentials) { // find user and verify password }, }), ], session: { strategy: 'database' }, callbacks: { async session({ session, user }) { session.user.id = user.id; return session; }, }, }); 

Important: for database sessions we provide PrismaAdapter, for JWT — not. Edge Runtime is automatically supported when using App Router.

Provider configuration

Providers are configured in the providers array. For OAuth, you need to specify clientId and clientSecret from the developer console. Credentials provider requires custom verification logic, but Auth.js handles session management and CSRF protection.

Route protection with middleware

// middleware.ts import { auth } from '@/auth'; export default auth((req) => { if (!req.auth) { const loginUrl = new URL('/login', req.url); loginUrl.searchParams.set('callbackUrl', req.url); return Response.redirect(loginUrl); } }); export const config = { matcher: ['/dashboard/:path*', '/settings/:path*'] }; 

Middleware applies to all routes in the matcher. This is a simple way to protect entire sections without code duplication.

JWT or database sessions: which to choose?

Criterion JWT Database sessions
Speed Fast (no DB request) Slower (request per query)
Security Less secure (reversible token) More secure (server-side session)
Scalability Easy (stateless) Harder (stateful, shared DB needed)
Session management Hard to revoke individually Easy via DB

Choice depends on your priorities. If speed and simplicity matter — JWT. If security and control — database sessions. In 70% of our projects we use database sessions with Prisma, as it gives more flexibility.

Process and timeline

Stage Result
Analysis Select providers, session strategy
Integration Configure OAuth, Credentials, email
Protection Middleware for routes, API
Test Check hydration, Edge
Documentation README, data schema

Basic integration (OAuth + JWT) takes 1–2 days. With database sessions and magic link — up to 4 days. Cost is calculated individually per project.

Typical mistakes during implementation

The most common mistake is wrong environment variables. AUTH_SECRET is mandatory, otherwise sessions will not be encrypted. Second issue — forgotten PrismaAdapter for database sessions, causing sessions not to persist. Third — missing session type typing, leading to TypeScript errors at compile time. In every project we check these points during code review.

Why trust authentication to professionals?

We have been doing web development for 5+ years and implemented authentication for 30+ Next.js projects. We guarantee stable operation and support after implementation. Contact us for a consultation on your project — we will select the optimal configuration and implement authentication in 1–4 days. Order Auth.js integration and get a ready-made solution with documentation and support.

Additional features

  • Email provider: magic link without password — convenient and secure.
  • Server Actions: permission checks directly on the server.
  • Custom login pages: your design under your brand.

With us you get not just code, but a proven architecture that withstands load and scales easily.