Nextauth

From G1Joshi/Agent-Skills · View interactive version

Nextauth is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert NextAuth.js / Auth.js assistance covering OAuth providers, credentials auth, database adapters, and session callbacks. Use when implementing authentication in Next.js, securing pages, or managing user sessions.

How to use this skill

Skill file

NextAuth.js (Auth.js)

NextAuth (evolving into Auth.js) is a complete open-source authentication solution. It is designed to work with any OAuth service, supports email/passwordless, and owns your data (Database Adapters).

When to Use

Quick Start

// auth.ts
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";

export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [GitHub],
});

// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;

Core Concepts

Auth.js Configuration Structure (v5)

Unified server configuration declared in auth.ts:

// auth.ts (NextAuth v5 / Auth.js)
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db } from "@/db";

export const { handlers, signIn, signOut, auth } = NextAuth({
  adapter: DrizzleAdapter(db),
  providers: [GitHub, Google],
  session: { strategy: "jwt" },
  callbacks: {
    jwt({ token, user }) {
      if (user) token.role = user.role;
      return token;
    },
    session({ session, token }) {
      session.user.role = token.role as string;
      return session;
    },
  },
});

Route Handler Integration (App Router)

Exports GET and POST handlers directly in the catch-all API route:

// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;

Server Component Authentication (`auth()`)

Inspects user session directly inside Server Components without client-side hooks:

// app/dashboard/page.tsx
import { auth } from "@/auth";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
  const session = await auth();
  if (!session?.user) redirect("/api/auth/signin");

  return (
    <div>
      Welcome back, {session.user.name}! (Role: {session.user.role})
    </div>
  );
}

Common Patterns

Session Enrichment via JWT Callbacks

Problem: The frontend session object misses custom user fields like roles or database IDs.

Solution:

Populate custom claims in jwt and forward them to the session callback:

export const authOptions: NextAuthOptions = {
  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.role = user.role;
        token.userId = user.id;
      }
      return token;
    },
    async session({ session, token }) {
      if (session.user) {
        session.user.role = token.role as string;
        session.user.id = token.userId as string;
      }
      return session;
    },
  },
};

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :-------------------- | :------------------------ | :------------------------------------------------------- |

| JWEDecryptionFailed | Wrong AUTH_SECRET. | Ensure AUTH_SECRET is set and consistent. |

| OAuthCallbackError | Provider config mismatch. | Check Authorised Redirect URIs in GitHub/Google console. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.