Files
image-apiv2/app/api/admin/users/route.ts
Beyhan Oğur 71eff2d979 first commit
2026-04-26 22:09:32 +03:00

58 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/lib/auth";
import { isAdmin } from "@/app/lib/permissions";
import { db } from "@/db";
import { user } from "@/db/schema";
import { desc } from "drizzle-orm";
/**
* GET /api/admin/users
* Tüm kullanıcıları listele (Sadece admin - Web Session)
*/
export async function GET(request: NextRequest) {
const session = await auth.api.getSession({
headers: request.headers,
});
if (!session?.user) {
return NextResponse.json({ error: "Giriş yapmalısınız" }, { status: 401 });
}
// Admin kontrolü
const userRole = (session.user as any).role || "user";
if (!isAdmin(userRole)) {
return NextResponse.json(
{ error: "Bu işlem için yetkiniz yok. Sadece adminler kullanıcıları görüntüleyebilir." },
{ status: 403 }
);
}
try {
const users = await db
.select({
id: user.id,
name: user.name,
email: user.email,
role: user.role,
emailVerified: user.emailVerified,
createdAt: user.createdAt,
})
.from(user)
.orderBy(desc(user.createdAt));
return NextResponse.json({
success: true,
data: {
users,
total: users.length,
},
});
} catch (error: any) {
console.error("Kullanıcı listesi hatası:", error);
return NextResponse.json(
{ error: "Kullanıcılar yüklenemedi" },
{ status: 500 }
);
}
}