first commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
'use server'
|
||||
|
||||
import { cookies } from 'next/headers'
|
||||
import { redirect } from 'next/navigation'
|
||||
|
||||
export async function login(prevState: any, formData: FormData) {
|
||||
const username = formData.get('username')
|
||||
const password = formData.get('password')
|
||||
|
||||
if (
|
||||
username === process.env.ADMIN_USERNAME &&
|
||||
password === process.env.ADMIN_PASSWORD
|
||||
) {
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.set('admin_session', process.env.ADMIN_SECRET || 'secret', {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7 // 1 week
|
||||
})
|
||||
|
||||
redirect('/admin')
|
||||
} else {
|
||||
return { error: 'Geçersiz kullanıcı adı veya şifre' }
|
||||
}
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
const cookieStore = await cookies()
|
||||
cookieStore.delete('admin_session')
|
||||
redirect('/admin/login')
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
'use server'
|
||||
|
||||
import { saveCategories, type Category } from '@/data/menu'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
async function checkAuth() {
|
||||
const cookieStore = await cookies()
|
||||
const session = cookieStore.get('admin_session')?.value
|
||||
if (!session || session !== (process.env.ADMIN_SECRET || 'secret')) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveEntireMenu(categories: Category[]) {
|
||||
await checkAuth()
|
||||
await saveCategories(categories)
|
||||
// Revalidate both language paths
|
||||
revalidatePath('/tr')
|
||||
revalidatePath('/en')
|
||||
return { success: true }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
'use server'
|
||||
|
||||
import { promises as fs } from 'fs'
|
||||
import path from 'path'
|
||||
import { cookies } from 'next/headers'
|
||||
|
||||
export async function uploadImage(formData: FormData) {
|
||||
const cookieStore = await cookies()
|
||||
const session = cookieStore.get('admin_session')?.value
|
||||
const secret = process.env.ADMIN_SECRET || 'secret'
|
||||
|
||||
if (!session || session !== secret) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
const file = formData.get('file') as File
|
||||
if (!file) {
|
||||
throw new Error('No file uploaded')
|
||||
}
|
||||
|
||||
const bytes = await file.arrayBuffer()
|
||||
const buffer = Buffer.from(bytes)
|
||||
|
||||
const extension = path.extname(file.name) || '.jpg'
|
||||
const filename = `${Date.now()}-${Math.random().toString(36).substring(7)}${extension}`
|
||||
|
||||
const uploadDir = path.join(process.cwd(), 'public', 'uploads')
|
||||
await fs.mkdir(uploadDir, { recursive: true })
|
||||
|
||||
const filePath = path.join(uploadDir, filename)
|
||||
await fs.writeFile(filePath, buffer)
|
||||
|
||||
return { url: `/uploads/${filename}` }
|
||||
}
|
||||
Reference in New Issue
Block a user