🔐 Clerk Auth

← Back to skills

> Clerk modern authentication rehberi.

Category: Frontend & UI/UX
Repo: vuralserhat86-antigravity-agentic-skills
Path: skills/clerk_auth/SKILL.md
Updated: 1/3/2026, 12:20:04 PM

AI Summary

> Clerk modern authentication rehberi. It is useful for React and Next.js, CSS and design systems, UI components, accessibility, and frontend polish. Source: vuralserhat86-antigravity-agentic-skills (skills/clerk_auth/SKILL.md).

🔐 Clerk Auth

Clerk modern authentication rehberi.


📋 Kurulum

npm install @clerk/nextjs

middleware.ts

import { clerkMiddleware } from '@clerk/nextjs/server';

export default clerkMiddleware();

export const config = {
  matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
};

🔧 Provider Setup

// app/layout.tsx
import { ClerkProvider } from '@clerk/nextjs';

export default function Layout({ children }) {
  return (
    <ClerkProvider>
      <html>
        <body>{children}</body>
      </html>
    </ClerkProvider>
  );
}

👤 Components

import { 
  SignInButton, 
  SignUpButton, 
  UserButton,
  SignedIn,
  SignedOut 
} from '@clerk/nextjs';

function Header() {
  return (
    <header>
      <SignedOut>
        <SignInButton />
        <SignUpButton />
      </SignedOut>
      <SignedIn>
        <UserButton />
      </SignedIn>
    </header>
  );
}

🔒 Server-side Auth

import { auth, currentUser } from '@clerk/nextjs/server';

export async function GET() {
  const { userId } = await auth();
  
  if (!userId) {
    return new Response('Unauthorized', { status: 401 });
  }
  
  const user = await currentUser();
  return Response.json({ user });
}

Clerk Auth v1.1 - Enhanced

🔄 Workflow

Kaynak: Clerk Documentation

Aşama 1: Integration

  • Install: @clerk/nextjs paketi ve API Key'ler.
  • Middleware: Public/Private rotaları clerkMiddleware ile ayır.
  • Provider: Root layout'u ClerkProvider ile sarmala.

Aşama 2: UX & Components

  • Header: SignedIn / SignedOut şartlı render yapısı kur.
  • Profile: UserButton veya UserProfile bileşenini ekle.
  • Custom Flow: Gerekirse Custom Sign-in sayfası yap.

Aşama 3: Server Logic

  • Protect: API rotalarında auth().userId kontrolü yap.
  • Data: currentUser() ile kullanıcı verisine eriş.
  • Sync: Webhook kullanarak kullanıcıyı kendi veritabanınla eşle (Opsiyonel).

Kontrol Noktaları

AşamaDoğrulama
1Middleware statik dosyaları (image, css) engellemiyor
2Sign-out sonrası login sayfasına yönlendiriyor
3API request'leri tokensiz atılınca 401 dönüyor

Related skills