### Development Environment Setup Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/architecture.md Steps to set up the development environment, including cloning the repository, installing dependencies, configuring environment variables, and running the development server. ```text Development: 1. Clone repo 2. pnpm install 3. Create .env.local with Clerk API keys 4. pnpm dev (starts on port 3000) 5. Navigate to http://localhost:3000 6. Click "Sign In" or "Sign Up" 7. Follow Clerk authentication flow 8. Redirected to home page with UserButton visible ``` -------------------------------- ### Clerk Next.js App Setup Steps Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/quick-reference.md A step-by-step guide to setting up a new Next.js project with Clerk authentication, including cloning the repository, installing dependencies, and configuring environment variables. ```bash # 1. Get Clerk API keys from https://dashboard.clerk.com/~/api-keys # 2. Clone repo git clone https://github.com/clerk/clerk-nextjs-app-quickstart cd clerk-nextjs-app-quickstart # 3. Install dependencies pnpm install # 4. Create .env.local with Clerk keys cp .env.example .env.local # Then edit .env.local with your keys # 5. Start dev server pnpm dev # Open http://localhost:3000 ``` -------------------------------- ### Start Production Server with pnpm Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/setup-guide.md Run this command to start the production server after installation and setup. This is typically used for deploying or testing the live application. ```bash pnpm start ``` -------------------------------- ### Copy Environment Example File Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/setup-guide.md Create a local environment file by copying the example configuration file. This is the first step before adding your Clerk API keys. ```bash cp .env.example .env.local ``` -------------------------------- ### Build and Start Production Server Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/setup-guide.md Commands to build the Next.js application for production and start a local server to test the production build. ```bash pnpm build pnpm start ``` -------------------------------- ### Clerk Setup and Development Commands Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/quick-reference.md Provides essential commands for setting up Clerk authentication in a Next.js project and managing the development environment. This includes installing dependencies, starting the development server, and configuring environment variables with API keys. ```bash # Development pnpm install # Install dependencies pnpm dev # Start dev server pnpm build # Build for production pnpm start # Run production build pnpm lint # Check code quality # Git git clone # Clone repo git add -A # Stage all changes git commit -m "msg" # Create commit git push # Push to GitHub # Clerk Setup # 1. Get keys from https://dashboard.clerk.com/~/api-keys # 2. cp .env.example .env.local # 3. Edit .env.local with your keys # 4. pnpm dev ``` -------------------------------- ### Clone the Quickstart Repository Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/README.md Clone the official Clerk Next.js App Router quickstart repository to your local machine. ```bash git clone https://github.com/clerk/clerk-nextjs-app-quickstart ``` -------------------------------- ### Complete Header Example in Root Layout Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/components.md This example demonstrates a complete header implementation in the root layout, showing sign-in/sign-up buttons when the user is signed out and the user button when the user is signed in. ```typescript
``` -------------------------------- ### Install Dependencies Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/README.md Install the necessary project dependencies using pnpm. A --force flag might be required due to React release candidates. ```bash pnpm install ``` -------------------------------- ### Install Prisma Client and Prisma CLI Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/deployment.md Install the necessary packages for Prisma ORM. This includes the client for database interactions and the CLI for migrations and schema management. ```bash pnpm add @prisma/client pnpm add -D prisma ``` -------------------------------- ### Clone the Clerk Next.js Quickstart Repository Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/setup-guide.md Use this command to clone the official Clerk Next.js application quickstart repository to your local machine. ```bash git clone https://github.com/clerk/clerk-nextjs-app-quickstart.git cd clerk-nextjs-app-quickstart ``` -------------------------------- ### Start the Next.js Development Server Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/setup-guide.md Launch the development server for your Next.js application using pnpm. This command will build the application and start a local server, typically at http://localhost:3000. ```bash pnpm dev ``` -------------------------------- ### Production Environment Setup on Vercel Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/architecture.md Instructions for deploying the application to Vercel, including setting environment variables and leveraging Vercel's automatic build and deployment process. ```text Production (Vercel): 1. Deploy repo to Vercel 2. Set environment variables in Vercel dashboard 3. Vercel automatically detects Next.js 4. Builds and deploys automatically ``` -------------------------------- ### Project Structure Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/overview.md Overview of the file and directory structure for the Clerk Next.js App Router quickstart. ```tree clerk-nextjs-app-quickstart/ ├── app/ │ ├── layout.tsx # Root layout with ClerkProvider │ ├── page.tsx # Home page component │ └── globals.css # Global styles ├── proxy.ts # Clerk middleware configuration ├── next.config.ts # Next.js configuration ├── tsconfig.json # TypeScript configuration ├── postcss.config.mjs # PostCSS configuration ├── eslint.config.js # ESLint configuration ├── package.json # Dependencies └── .env.example # Environment variables template ``` -------------------------------- ### Next.js Configuration Usage Example Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/types.md Example of how to declare and export a Next.js configuration object using the NextConfig type. ```typescript const nextConfig: NextConfig = { /* config options here */ }; export default nextConfig; ``` -------------------------------- ### Environment Variables Example Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/file-structure.md Template for environment variables, including Clerk publishable and secret keys. Copy this to .env.local and fill in actual values. ```bash NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= CLERK_SECRET_KEY= ``` -------------------------------- ### Next.js Metadata Usage Example Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/types.md Example of defining metadata for a Next.js page, including title and description. ```typescript export const metadata: Metadata = { title: 'Clerk Next.js Quickstart', description: 'Generated by create next app', } ``` -------------------------------- ### Complete Next.js Layout with Clerk Components Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/clerk-components-reference.md A full example of app/layout.tsx demonstrating the integration of ClerkProvider, SignInButton, SignUpButton, Show, and UserButton for a complete authentication UI. ```typescript // app/layout.tsx import type { Metadata } from 'next' import { ClerkProvider, SignInButton, SignUpButton, Show, UserButton, } from '@clerk/nextjs' import './globals.css' export const metadata: Metadata = { title: 'Clerk Next.js Quickstart', description: 'Generated by create next app', } export default function RootLayout({ children }: { children: React.ReactNode }) { return (
{children}
) } ``` -------------------------------- ### Getting User Data in Client and Server Components Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/quick-reference.md Provides examples for fetching user data using `useUser` hook in client components and `currentUser` function in server components. ```typescript // Client component import { useUser } from '@clerk/nextjs'; const { user } = useUser(); // user.firstName, user.emailAddresses[0].emailAddress, etc. // Server component import { currentUser } from '@clerk/nextjs/server'; const user = await currentUser(); // Same user object ``` -------------------------------- ### Complete Example: API Route and Client Form Source: https://github.com/clerk/clerk-nextjs-app-quickstart/blob/main/_autodocs/clerk-hooks-and-helpers.md Demonstrates combining server-side protection in an API route with client-side checks and authenticated requests using `useAuth()` in a form component. ```typescript // app/api/posts/route.ts import { auth } from '@clerk/nextjs/server'; export async function POST(req: Request) { const { userId, protect } = await auth(); // Require authentication protect(); const body = await req.json(); // Create post for authenticated user const post = await db.posts.create({ data: { title: body.title, content: body.content, authorId: userId, } }); return Response.json(post); } // app/components/post-form.tsx 'use client'; import { useAuth } from '@clerk/nextjs'; import { useState } from 'react'; export function PostForm() { const { isSignedIn } = useAuth(); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); if (!isSignedIn) { return

Sign in to create posts

; } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); const response = await fetch('/api/posts', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, content }) }); if (response.ok) { setTitle(''); setContent(''); // Refresh posts list } } return (
setTitle(e.target.value)} placeholder="Title" />