authjs
Assists with adding authentication to web applications using Auth.js (formerly NextAuth.js). Use when configuring OAuth providers, database sessions, JWT strategies, role-based access, or multi-tenant auth in Next.js, SvelteKit, Express, or other frameworks. Trigger words: authjs, nextauth, oauth, authentication, login, session, providers.
Usage
Getting Started
- Install the skill using the command above
- Open your AI coding agent (Claude Code, Codex, Gemini CLI, or Cursor)
- Reference the skill in your prompt
- The AI will use the skill's capabilities automatically
Example Prompts
- "Review the open pull requests and summarize what needs attention"
- "Generate a changelog from the last 20 commits on the main branch"
Documentation
Overview
Auth.js is a universal authentication library for web applications, supporting 80+ OAuth providers, credentials-based login, magic links, and WebAuthn passkeys. It integrates with Next.js, SvelteKit, Express, and other frameworks, offering both JWT and database-backed session strategies with customizable callbacks for role-based access and multi-tenant architectures.
Instructions
- When setting up authentication, create an
auth.tsconfig file with providers, adapter, and callbacks, then wire it into the framework's route handler or middleware. - When choosing a session strategy, use JWT for stateless apps (marketing sites, public APIs) and database sessions for apps requiring session revocation (admin panels, banking).
- When configuring OAuth providers, add the desired providers (Google, GitHub, Discord, etc.) and handle the
signIncallback to control access and account linking. - When customizing sessions, always add
user.idto thesessioncallback since it is not included by default, and add any custom fields likeroleortenantIdin the JWT callback. - When protecting routes, use
auth()in Server Components, Route Handlers, and Server Actions; do not rely solely on middleware for authorization. - When building custom login pages, set
pages: { signIn: "/login" }in the config to replace the default Auth.js page with a branded UI. - When integrating a database, use the appropriate adapter (
@auth/prisma-adapter,@auth/drizzle-adapter,@auth/mongodb-adapter) or implement theAdapterinterface for custom databases.
Examples
Example 1: Add Google and GitHub OAuth to a Next.js app
User request: "Set up Auth.js with Google and GitHub login in my Next.js app"
Actions:
- Install
next-authand configureauth.tswith Google and GitHub providers - Set up the session callback to include
user.idanduser.role - Add
middleware.tsto protect authenticated routes - Create a custom login page with provider sign-in buttons
Output: A Next.js app with OAuth login via Google and GitHub, protected routes, and a custom sign-in page.
Example 2: Add role-based access control with database sessions
User request: "Implement admin and user roles with database sessions"
Actions:
- Set up Prisma adapter with a User model that includes a
rolefield - Configure database session strategy in
auth.ts - Add the
roleto the JWT and session callbacks - Create middleware that checks
session.user.rolebefore granting access to admin routes
Output: A role-based auth system where admins and users see different content, backed by database sessions for revocability.
Guidelines
- Always customize the
sessioncallback to includeuser.idsince it is not included by default. - Protect API routes and Server Actions with
auth()rather than relying only on middleware. - Store provider tokens in the JWT callback if you need to call provider APIs (GitHub, Google Calendar).
- Keep
AUTH_SECRETin environment variables and rotate periodically; never expose session secrets. - Use
ScryptorArgon2idfor password hashing in credentials-based flows. - Handle errors gracefully: implement error pages for
OAuthCallbackErrorandAccessDeniedscenarios.
Information
- Version
- 1.0.0
- Author
- terminal-skills
- Category
- Development
- License
- Apache-2.0