hata ayıklama ve kod temizliği

This commit is contained in:
2026-01-11 23:58:09 +03:00
parent b2a915240f
commit 32009b4886
22 changed files with 117 additions and 92 deletions

View File

@@ -3,7 +3,6 @@
import { createClient } from "@/lib/supabase-server"
import { createClient as createSupabaseClient } from "@supabase/supabase-js"
import { revalidatePath } from "next/cache"
import { redirect } from "next/navigation"
// WARNING: specialized client for admin actions only
// This requires SUPABASE_SERVICE_ROLE_KEY to be set in .env.local
@@ -19,20 +18,12 @@ const supabaseAdmin = createSupabaseClient(
)
export async function createUser(firstName: string, lastName: string, email: string, password: string, role: 'admin' | 'user', phone?: string) {
const supabase = createClient()
// 1. Check if current user is admin
const { data: { user: currentUser } } = await supabase.auth.getUser()
if (!currentUser) return { error: "Oturum açmanız gerekiyor." }
const { data: profile } = await supabase
.from('profiles')
.select('role')
.eq('id', currentUser.id)
.single()
if (!profile || profile.role !== 'admin') {
return { error: "Yetkisiz işlem. Sadece yöneticiler kullanıcı oluşturabilir." }
try {
await assertAdmin()
} catch (error) {
return { error: (error as Error).message }
}
// 2. Create user using Admin client
@@ -74,14 +65,13 @@ export async function createUser(firstName: string, lastName: string, email: str
}
export async function deleteUser(userId: string) {
const supabase = createClient()
// Check admin
const { data: { user: currentUser } } = await supabase.auth.getUser()
if (!currentUser) return { error: "Oturum açmanız gerekiyor." }
const { data: profile } = await supabase.from('profiles').select('role').eq('id', currentUser.id).single()
if (profile?.role !== 'admin') return { error: "Yetkisiz işlem." }
try {
await assertAdmin()
} catch (error: any) {
return { error: error.message }
}
// Delete user
const { error } = await supabaseAdmin.auth.admin.deleteUser(userId)
@@ -93,15 +83,13 @@ export async function deleteUser(userId: string) {
}
export async function updateUser(userId: string, data: { firstName: string, lastName: string, email: string, password?: string, role: 'admin' | 'user', phone?: string }) {
const supabase = createClient()
// Check admin
const { data: { user: currentUser } } = await supabase.auth.getUser()
if (!currentUser) return { error: "Oturum açmanız gerekiyor." }
// Check if current user is admin
const { data: profile } = await supabase.from('profiles').select('role').eq('id', currentUser.id).single()
if (profile?.role !== 'admin') return { error: "Yetkisiz işlem." }
try {
await assertAdmin()
} catch (error: any) {
return { error: error.message }
}
// 1. Update Profile (Role and Name)
const { error: profileError } = await supabaseAdmin
@@ -116,7 +104,7 @@ export async function updateUser(userId: string, data: { firstName: string, last
if (profileError) return { error: "Profil güncellenemedi: " + profileError.message }
// 2. Update Auth (Email and Password)
const authUpdates: any = {
const authUpdates: { email: string; user_metadata: { full_name: string }; password?: string } = {
email: data.email,
user_metadata: {
full_name: `${data.firstName} ${data.lastName}`.trim()
@@ -162,3 +150,21 @@ export async function updateProfile(data: { firstName: string, lastName: string,
revalidatePath("/dashboard/profile")
return { success: true }
}
async function assertAdmin() {
const supabase = createClient()
const { data: { user: currentUser } } = await supabase.auth.getUser()
if (!currentUser) throw new Error("Oturum açmanız gerekiyor.")
const { data: profile } = await supabase
.from('profiles')
.select('role')
.eq('id', currentUser.id)
.single()
if (!profile || profile.role !== 'admin') {
throw new Error("Yetkisiz işlem. Sadece yöneticiler bu işlemi gerçekleştirebilir.")
}
return currentUser
}