hata ayıklama ve kod temizliği
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { createClient } from "@/lib/supabase-server"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { DollarSign, ShoppingCart, Users, CreditCard, Package } from "lucide-react"
|
||||
import { DollarSign, ShoppingCart, Users, Package } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { Button } from "@/components/ui/button"
|
||||
|
||||
|
||||
@@ -3,7 +3,15 @@
|
||||
import { createClient } from "@/lib/supabase-server"
|
||||
import { revalidatePath } from "next/cache"
|
||||
|
||||
export async function createProduct(data: any) {
|
||||
interface ProductData {
|
||||
name: string
|
||||
category: string
|
||||
description?: string
|
||||
price: number
|
||||
image_url?: string
|
||||
}
|
||||
|
||||
export async function createProduct(data: ProductData) {
|
||||
const supabase = createClient()
|
||||
|
||||
// Validate data manually or use Zod schema here again securely
|
||||
@@ -23,12 +31,12 @@ export async function createProduct(data: any) {
|
||||
|
||||
revalidatePath("/dashboard/products")
|
||||
return { success: true }
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message }
|
||||
} catch (error) {
|
||||
return { success: false, error: (error as Error).message }
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProduct(id: number, data: any) {
|
||||
export async function updateProduct(id: number, data: ProductData) {
|
||||
const supabase = createClient()
|
||||
|
||||
try {
|
||||
@@ -45,7 +53,7 @@ export async function updateProduct(id: number, data: any) {
|
||||
revalidatePath("/dashboard/products")
|
||||
revalidatePath(`/dashboard/products/${id}`)
|
||||
return { success: true }
|
||||
} catch (error: any) {
|
||||
return { success: false, error: error.message }
|
||||
} catch (error) {
|
||||
return { success: false, error: (error as Error).message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createClient } from "@/lib/supabase-server"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
|
||||
import { UserForm } from "@/components/dashboard/user-form"
|
||||
import { notFound } from "next/navigation"
|
||||
|
||||
import { getProfile } from "@/lib/data"
|
||||
|
||||
export default async function ProfilePage() {
|
||||
@@ -23,9 +23,17 @@ export default async function ProfilePage() {
|
||||
return <div>Profil verisi bulunamadı.</div>
|
||||
}
|
||||
|
||||
const parts = (profile.full_name || "").split(' ')
|
||||
const firstName = parts[0] || ""
|
||||
const lastName = parts.slice(1).join(' ') || ""
|
||||
// Improved name parsing logic
|
||||
const fullName = (profile.full_name || "").trim()
|
||||
const firstSpaceIndex = fullName.indexOf(' ')
|
||||
|
||||
let firstName = fullName
|
||||
let lastName = ""
|
||||
|
||||
if (firstSpaceIndex > 0) {
|
||||
firstName = fullName.substring(0, firstSpaceIndex)
|
||||
lastName = fullName.substring(firstSpaceIndex + 1)
|
||||
}
|
||||
|
||||
const initialData = {
|
||||
firstName,
|
||||
|
||||
@@ -2,8 +2,7 @@ import { createClient } from "@/lib/supabase-server"
|
||||
import { SiteSettingsForm } from "@/components/dashboard/site-settings-form"
|
||||
import { AppearanceForm } from "@/components/dashboard/appearance-form"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Switch } from "@/components/ui/switch"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
|
||||
export default async function SettingsPage() {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export default function ContactPage() {
|
||||
} else {
|
||||
toast.error("Hata: " + response.error)
|
||||
}
|
||||
} catch (error) {
|
||||
} catch {
|
||||
toast.error("Bir hata oluştu.")
|
||||
} finally {
|
||||
setIsSubmitting(false)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import Image from "next/image"
|
||||
|
||||
|
||||
export default function CorporatePage() {
|
||||
return (
|
||||
@@ -8,7 +8,7 @@ export default function CorporatePage() {
|
||||
<section className="space-y-6 text-center">
|
||||
<h1 className="text-4xl md:text-5xl font-bold tracking-tight font-outfit">Hakkımızda</h1>
|
||||
<p className="text-xl text-muted-foreground">
|
||||
1995'ten beri güvenliğinizi en değerli hazinemiz olarak görüyoruz.
|
||||
1995'ten beri güvenliğinizi en değerli hazinemiz olarak görüyoruz.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
@@ -28,7 +28,7 @@ export default function CorporatePage() {
|
||||
</p>
|
||||
<h2 className="text-3xl font-bold pt-4">Vizyonumuz</h2>
|
||||
<p className="text-slate-600 dark:text-slate-300 leading-relaxed">
|
||||
Türkiye'nin lider çelik kasa üreticisi olarak, global pazarda güvenilirlik ve kalite
|
||||
Türkiye'nin lider çelik kasa üreticisi olarak, global pazarda güvenilirlik ve kalite
|
||||
denince akla gelen ilk marka olmak. Yenilikçi Ar-Ge çalışmalarımızla güvenlik teknolojilerine
|
||||
yön vermek.
|
||||
</p>
|
||||
|
||||
@@ -36,7 +36,7 @@ export default function LoginPage() {
|
||||
|
||||
router.push("/dashboard")
|
||||
router.refresh()
|
||||
} catch (err: any) {
|
||||
} catch {
|
||||
setError("Bir hata oluştu. Lütfen tekrar deneyin.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Link from "next/link"
|
||||
import Image from "next/image"
|
||||
import { ArrowRight, ShieldCheck, Lock, Award, History, LayoutDashboard, LogIn } from "lucide-react"
|
||||
import { ArrowRight, ShieldCheck, Lock, History, LayoutDashboard } from "lucide-react"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { createClient } from "@/lib/supabase-server"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Card, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import Image from "next/image"
|
||||
|
||||
|
||||
const products = [
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
import Link from "next/link"
|
||||
import { supabase } from "@/lib/supabase"
|
||||
import { Button } from "@/components/ui/button"
|
||||
@@ -11,7 +11,6 @@ import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
|
||||
import { AlertCircle, Loader2, CheckCircle2 } from "lucide-react"
|
||||
|
||||
export default function SignUpPage() {
|
||||
const router = useRouter()
|
||||
const [email, setEmail] = useState("")
|
||||
const [password, setPassword] = useState("")
|
||||
const [loading, setLoading] = useState(false)
|
||||
@@ -76,7 +75,7 @@ export default function SignUpPage() {
|
||||
<CardHeader className="space-y-1">
|
||||
<CardTitle className="text-2xl font-bold">Hesap Oluştur</CardTitle>
|
||||
<CardDescription>
|
||||
ParaKasa'ya katılmak için bilgilerinizi girin
|
||||
ParaKasa'ya katılmak için bilgilerinizi girin
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
import { Inter, Outfit } from "next/font/google";
|
||||
import { Toaster } from "sonner";
|
||||
import "./globals.css";
|
||||
|
||||
Reference in New Issue
Block a user