Observed since 20 Oct, 22:11 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
CLI tool to automatically setup NextAuth.js authentication in Next.js App Router projects
A CLI tool to automatically setup NextAuth.js authentication in Next.js App Router projects.
next-auth@beta if not already presentsrc/ directory, App Router structure, and package manager (bun/npm/yarn/pnpm)Note: This package currently generates Google OAuth setup. Support for multiple providers (GitHub, Discord, Credentials, etc.) and custom configuration files is planned. See TODO.md for the full roadmap.
bun add -d @0ju1c3-oss/next-auth-cli
or
npm install -D @0ju1c3-oss/next-auth-cli
bun add -g @0ju1c3-oss/next-auth-cli
Navigate to your Next.js project root and run:
bunx next-auth-setup
or with npx:
npx @0ju1c3-oss/next-auth-cli
The CLI creates the following files in your project:
auth.ts (or src/auth.ts) - NextAuth.js configuration (currently: Google OAuth provider)middleware.ts (or src/middleware.ts) - Route protection for /dashboard/* and /profile/*app/api/auth/[...nextauth]/route.ts - NextAuth.js route handlercomponents/login-button.tsx - Sign in/out button componentcomponents/user-profile.tsx - User profile display component.env.local.example - Template for required environment variablesAfter running the CLI, follow these steps:
Note: The CLI automatically installs
next-auth@betafor you if it's not already in your project!
Create a .env.local file:
cp .env.local.example .env.local
Add your credentials:
AUTH_SECRET=your-secret-key-here
AUTH_GOOGLE_ID=your-google-client-id
AUTH_GOOGLE_SECRET=your-google-client-secret
Generate a secret:
openssl rand -base64 32
http://localhost:3000/api/auth/callback/google
.env.localUpdate your app/layout.tsx:
import { SessionProvider } from "next-auth/react"
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<SessionProvider>
{children}
</SessionProvider>
</body>
</html>
)
}
import LoginButton from '@/components/login-button'
import UserProfile from '@/components/user-profile'
export default function Page() {
return (
<div>
<LoginButton />
<UserProfile />
</div>
)
}
The generated middleware protects these routes by default:
/dashboard/* - Dashboard pages/profile/* - Profile pagesUnauthenticated users will be redirected to the sign-in page.
Edit middleware.ts to add or remove protected routes:
export const config = {
matcher: ["/dashboard/:path*", "/profile/:path*", "/admin/:path*"],
}
The CLI automatically detects and adapts to your project structure:
src/ directoryyour-project/
├── src/
│ ├── app/
│ │ └── api/auth/[...nextauth]/route.ts
│ ├── auth.ts
│ └── middleware.ts
└── components/
├── login-button.tsx
└── user-profile.tsx
src/ directoryyour-project/
├── app/
│ └── api/auth/[...nextauth]/route.ts
├── auth.ts
├── middleware.ts
└── components/
├── login-button.tsx
└── user-profile.tsx
Ensure your package.json includes next as a dependency:
{
"dependencies": {
"next": "^14.0.0"
}
}
This CLI requires Next.js App Router (app/ directory). Pages Router is not currently supported.
Make sure your Google OAuth redirect URI matches exactly:
http://localhost:3000/api/auth/callback/google
For production, add:
https://yourdomain.com/api/auth/callback/google
ISC
See TODO.md for planned features including:
next-auth-cli.json)Issues and pull requests are welcome! Visit the GitHub repository.
Download counts and popularity will appear here as the collector receives them.
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 22:11 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:24687c6616b118e714bdbad5420087ca3ecd2351bbfe3d6462f624f70a8113c9