---
title: OAuth Authorize API
description: OAuth yetkilendirme akışını başlatan API endpoint implementasyonu
---

OAuth yetkilendirme akışını başlatan ilk API endpoint'idir.

## Dosya Yapısı

`app/api/oauth/authorize/route.ts` dosyasını oluşturun:

```
app/
├── api/
│   └── oauth/
│       └── authorize/
│           └── route.ts
```

<Callout type="info" title="Next.js App Router">
Bu implementasyon Next.js 15 App Router kullanır. Eğer Pages Router kullanıyorsanız `pages/api/oauth/authorize.ts` şeklinde oluşturmanız gerekir.
</Callout>

## API Implementation

```typescript title="File: app/api/oauth/authorize/route.ts"
import { NextRequest, NextResponse } from 'next/server';
import { OAuthAPI } from '@ikas/api-client';
import { config } from '@/globals/config';
import { RedisDB } from '@/lib/redis';

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams;
    const storeName = searchParams.get('storeName');

    // storeName parametresini doğrula
    if (!storeName) {
      return NextResponse.json(
        { error: 'storeName is required' },
        { status: 400 }
      );
    }

    // CSRF saldırılarını önlemek için state değişkeni oluştur
    const state = Math.random().toFixed(16);

    // State'i geçici olarak kaydet (60 saniye TTL)
    await RedisDB.state.set(state, state, 60);

    // OAuth URL oluştur
    const oauthUrl = OAuthAPI.getOAuthUrl({ storeName });
    const authorizeUrl = new URL(`${oauthUrl}/authorize`);
    
    authorizeUrl.searchParams.set('client_id', config.appId);
    authorizeUrl.searchParams.set('redirect_uri', config.callbackUrl);
    authorizeUrl.searchParams.set('scope', config.scope);
    authorizeUrl.searchParams.set('state', state);

    // ikas dashboard'a yönlendir
    return NextResponse.redirect(authorizeUrl.toString());

  } catch (error: any) {
    console.error('Authorize error:', error);
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    );
  }
}
```

## Kod Açıklaması

### Request Validation
```typescript
const storeName = searchParams.get('storeName');
if (!storeName) {
  return NextResponse.json({ error: 'storeName is required' }, { status: 400 });
}
```

API `storeName` query parametresi bekler. Bu mağazanızın subdomain'idir. Örneğin `mystore.myikas.com` için `mystore` olmalıdır.

### State Oluşturma
```typescript
const state = Math.random().toFixed(16);
await RedisDB.state.set(state, state, 60);
```

CSRF saldırılarını önlemek için `state` değişkeni oluştururuz. Bu değişken 10 karakterden uzun olmalı ve callback'te doğrulanmalıdır. 60 saniye TTL ile Redis'e kaydediyoruz.

### OAuth URL Oluşturma
```typescript
const oauthUrl = OAuthAPI.getOAuthUrl({ storeName });
const authorizeUrl = new URL(`${oauthUrl}/authorize`);

authorizeUrl.searchParams.set('client_id', config.appId);
authorizeUrl.searchParams.set('redirect_uri', config.callbackUrl);
authorizeUrl.searchParams.set('scope', config.scope);
authorizeUrl.searchParams.set('state', state);
```

`@ikas/api-client` kütüphanesinin `getOAuthUrl` helper fonksiyonu ile OAuth URL'i oluştururuz. Sonra gerekli parametreleri ekleyip yetkilendirme URL'ini tamamlarız.

### Yönlendirme
```typescript
return NextResponse.redirect(authorizeUrl.toString());
```

Kullanıcıyı ikas dashboard'daki yetkilendirme sayfasına yönlendiriyoruz.

<Callout type="info" title="OAuth Akışı">
Bu API çağrıldığında kullanıcı ikas dashboard'da "Uygulama Erişimi Ver" sayfasını görecek ve uygulamanızı onaylayabilecektir.
</Callout>