# Authentication Kurulum Kılavuzu Bu Next.js projesi Django REST API ile entegre çalışan tam özellikli bir authentication sistemi içerir. ## 📋 Özellikler ✅ Email/Password ile kayıt ve giriş ✅ Email aktivasyonu ✅ Social Auth (Google, GitHub) ✅ Token refresh (JWT) ✅ Şifre sıfırlama ✅ Kullanıcı profili ✅ Session yönetimi ## 🚀 Kurulum ### 1. Environment Variables `.env.local` dosyası oluşturun (`.env.example` dosyasını kopyalayın): ```bash cp .env.example .env.local ``` `.env.local` dosyasını düzenleyin: ```env # NextAuth Configuration NEXTAUTH_URL=http://localhost:3000 NEXTAUTH_SECRET=super-secret-key-change-this # Django REST API NEXT_PUBLIC_API_BASE_URL=http://localhost:8000/api/v1 # Social Auth (İsteğe bağlı) GOOGLE_ID=your-google-client-id GOOGLE_SECRET=your-google-client-secret GITHUB_ID=your-github-client-id GITHUB_SECRET=your-github-client-secret ``` ### 2. NextAuth Secret Üretme ```bash openssl rand -base64 32 ``` Bu komutu çalıştırın ve çıkan değeri `NEXTAUTH_SECRET` olarak kullanın. ### 3. Django Backend Django backend'inizin çalıştığından emin olun: ```bash # Django projenizde python manage.py runserver ``` Backend `http://localhost:8000` adresinde çalışıyor olmalı. ## 📱 Sayfalar ve Rotalar ### Authentication Sayfaları | Sayfa | Route | Açıklama | |-------|-------|----------| | Login | `/auth/login` | Giriş sayfası (Email/Password + Social) | | Register | `/auth/register` | Kayıt sayfası | | Activate | `/auth/activate/[uid]/[token]` | Email aktivasyon sayfası | | Resend Activation | `/auth/resend-activation` | Aktivasyon emaili tekrar gönderme | | Password Reset | `/auth/password-reset` | Şifre sıfırlama talebi | | Reset Confirm | `/auth/password-reset/confirm/[uid]/[token]` | Şifre sıfırlama onayı | | Error | `/auth/error` | Auth hata sayfası | ### Korumalı Sayfalar | Sayfa | Route | Açıklama | |-------|-------|----------| | Profile | `/profile` | Kullanıcı profili | | Dashboard | `/dashboard` | Dashboard (mevcut) | ## 🔐 Social Authentication Kurulumu ### Google OAuth2 1. [Google Cloud Console](https://console.cloud.google.com/) adresine gidin 2. Yeni proje oluşturun veya mevcut projeyi seçin 3. "APIs & Services" > "Credentials" sayfasına gidin 4. "Create Credentials" > "OAuth 2.0 Client ID" seçin 5. Application type: "Web application" 6. Authorized redirect URIs: - `http://localhost:3000/api/auth/callback/google` - `https://yourdomain.com/api/auth/callback/google` (production) 7. Client ID ve Client Secret'i kopyalayıp `.env.local` dosyasına ekleyin ### GitHub OAuth2 1. [GitHub Developer Settings](https://github.com/settings/developers) adresine gidin 2. "New OAuth App" butonuna tıklayın 3. Form bilgileri: - Application name: "Your App Name" - Homepage URL: `http://localhost:3000` - Authorization callback URL: `http://localhost:3000/api/auth/callback/github` 4. Client ID ve Client Secret'i `.env.local` dosyasına ekleyin ## 🔄 Authentication Flow ### 1. Kayıt (Register) ``` User fills form → POST /auth/users/ → Email sent → User clicks link → Account activated → User can login ``` ### 2. Login (Email/Password) ``` User enters credentials → POST /auth/jwt/create/ → Tokens received → Session created → User authenticated ``` ### 3. Social Login ``` User clicks Google/GitHub → OAuth flow → Access token received → POST /auth/social/{provider}/ → Tokens received → Session created ``` ### 4. Token Refresh ``` Access token expires → Auto refresh with refresh token → New tokens received → Session updated ``` ## 🛡️ Middleware ve Koruma ### Session Kontrolü ```tsx import { useSession } from "next-auth/react"; export default function ProtectedPage() { const { data: session, status } = useSession(); if (status === "loading") { return