first commit
This commit is contained in:
96
app/api/auth/cookie-login/route.ts
Normal file
96
app/api/auth/cookie-login/route.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import {
|
||||
COOKIE_ACCESS,
|
||||
COOKIE_REFRESH,
|
||||
COOKIE_OPTS,
|
||||
ACCESS_MAX_AGE,
|
||||
REFRESH_MAX_AGE,
|
||||
} from "@/lib/auth-cookies";
|
||||
|
||||
const BASE_URL =
|
||||
process.env.BASE_API_URL ??
|
||||
process.env.NEXT_PUBLIC_BASE_API_URL ??
|
||||
"http://127.0.0.1:8080";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password } = body as { email?: string; password?: string };
|
||||
if (!email || !password) {
|
||||
return NextResponse.json(
|
||||
{ error: "E-posta ve şifre gerekli." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(`${BASE_URL}/api/v1/auth/login`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
accept: "application/json",
|
||||
},
|
||||
body: JSON.stringify({ email: String(email).trim(), password }),
|
||||
});
|
||||
} catch (fetchErr) {
|
||||
const msg =
|
||||
process.env.NODE_ENV === "development" && fetchErr instanceof Error
|
||||
? `Backend erişilemedi: ${fetchErr.message} (URL: ${BASE_URL})`
|
||||
: "Giriş servisi şu an kullanılamıyor.";
|
||||
return NextResponse.json({ error: msg }, { status: 502 });
|
||||
}
|
||||
|
||||
let data: unknown;
|
||||
try {
|
||||
const text = await res.text();
|
||||
data = text ? JSON.parse(text) : {};
|
||||
} catch {
|
||||
data = {};
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const message =
|
||||
(data as { detail?: string })?.detail ?? "Giriş başarısız";
|
||||
return NextResponse.json(
|
||||
{ error: message },
|
||||
{ status: res.status >= 400 ? res.status : 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const access_token = (data as { access_token?: string })?.access_token;
|
||||
const refresh_token = (data as { refresh_token?: string })?.refresh_token;
|
||||
const user = (data as { user?: unknown })?.user;
|
||||
|
||||
if (!access_token || !refresh_token) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
process.env.NODE_ENV === "development"
|
||||
? "Backend token döndürmedi."
|
||||
: "Giriş yanıtı geçersiz.",
|
||||
},
|
||||
{ status: 502 }
|
||||
);
|
||||
}
|
||||
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set(COOKIE_ACCESS, access_token, {
|
||||
...COOKIE_OPTS,
|
||||
maxAge: ACCESS_MAX_AGE,
|
||||
});
|
||||
cookieStore.set(COOKIE_REFRESH, refresh_token, {
|
||||
...COOKIE_OPTS,
|
||||
maxAge: REFRESH_MAX_AGE,
|
||||
});
|
||||
|
||||
return NextResponse.json({ user });
|
||||
} catch (e) {
|
||||
const message =
|
||||
process.env.NODE_ENV === "development" && e instanceof Error
|
||||
? e.message
|
||||
: "Sunucu hatası.";
|
||||
return NextResponse.json({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user