Back to Blog
Complete Auth JS guide for Next.js

Complete Auth JS guide for Next.js

March 6, 2025 (1mo ago)

This post provides a detailed, step-by-step guide to implementing NextAuth.js (previously known as Next Auth) in your Next.js projects. It covers the essential concepts and strategies involved.


Prerequisites :


Step-by-Step Implementation

  1. Install NextAuth.js :

    npm install next-auth
  2. Define Types (TypeScript) :

    When using TypeScript, you can define the types for sessions and users by creating interfaces for the session and user objects. Here's an example:

To create a

next-auth.d.ts
file in your project root directory, follow these steps:

  1. Navigate to your project root directory : Use your terminal or file explorer to go to the root directory of your Next.js project.

  2. Create the file : Create a new file named

    next-auth.d.ts
    .

  3. Define the types : Add the following TypeScript definitions to the file to extend the default NextAuth.js types

import NextAuth from "next-auth";

declare module "next-auth" {
  interface User {
    id: string;
    role: string; // e.g., 'admin', 'user'
  }

  interface Session {
    user: User;
    expires: string; // ISO date string
  }
}
  1. Configure NextAuth.js Options :

Create a file, such as auth.ts or auth.js, to configure NextAuth.js. The configuration includes


Secret : A secret key used to encrypt the session

import { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions: NextAuthOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "Email", type: "text" },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials) {
        // Write a code for authorize user
      },
    }),
  ],
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.id = user.id;
      }
      return token;
    },
    async session({ session, token }) {
      if (session.user) {
        session.user.id = token.id as string;
      }
      return session;
    },
  },
  pages: {
    signIn: "/login",
    error: "/login",
  },
  session: {
    strategy: "jwt",
    maxAge: 30 * 24 * 60 * 60,
  },
  secret: process.env.NEXTAUTH_SECRET, // nextAuth Secret Key
};

Diagram :

4. Create API Endpoint :

Create a file [...nextauth].ts inside the pages/api/auth directory. This file handles the authentication routes. Import the Next Auth function and pass the configuration options

5. Register User :

NextAuth.js is primarily for authentication, not registration. Implement a custom registration strategy using a credential provider. In the registration logic, check if the user already exists, and if not, create a new user in the database


Key Concepts


Conclusion

This guide provides a comprehensive overview of implementing NextAuth.js for authentication in Next.js applications. Remember that NextAuth.js handles authentication, while user registration requires a custom implementation

If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on JavaScript, React.js, Next.js, and other web Development topics.

For Paid collaboration, Web Development freelancing work, mail me at: krishdesai044@gmail.com

Connect with me on Twitter, LinkedIn, and GitHub.

Thank you for Reading

Happy Coding

Thank You