notification + admin panel

This commit is contained in:
root
2025-12-21 11:39:41 +01:00
parent 514e04f43d
commit 5e65144934
15 changed files with 2423 additions and 883 deletions

58
lib/admin-auth.ts Normal file
View File

@@ -0,0 +1,58 @@
import { cookies } from 'next/headers'
const ADMIN_PASSWORD = 'HelloWorld'
const ADMIN_SESSION_COOKIE = 'admin_session'
// Check if admin is authenticated
export async function isAdminAuthenticated(): Promise<boolean> {
try {
const cookieStore = await cookies()
const session = cookieStore.get(ADMIN_SESSION_COOKIE)?.value
return session === 'authenticated'
} catch (error) {
return false
}
}
// Verify admin password
export function verifyAdminPassword(password: string): boolean {
return password === ADMIN_PASSWORD
}
// Set admin session
export async function setAdminSession(): Promise<void> {
const cookieStore = await cookies()
cookieStore.set(ADMIN_SESSION_COOKIE, 'authenticated', {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 60 * 60 * 24, // 24 hours
})
}
// Clear admin session
export async function clearAdminSession(): Promise<void> {
const cookieStore = await cookies()
cookieStore.delete(ADMIN_SESSION_COOKIE)
}
// Get admin session from request (for API routes)
export function getAdminSessionFromRequest(request: Request): boolean {
try {
const cookieHeader = request.headers.get('cookie')
if (!cookieHeader) {
return false
}
const cookies = cookieHeader.split(';').reduce((acc, cookie) => {
const [key, value] = cookie.trim().split('=')
acc[key] = value
return acc
}, {} as Record<string, string>)
return cookies[ADMIN_SESSION_COOKIE] === 'authenticated'
} catch (error) {
return false
}
}