mirror of
https://github.com/R0m1k3/Menu.git
synced 2026-10-19 05:06:32 +02:00
feat: initialize project with Next.js, Prisma, and admin dashboard functionality
This commit is contained in:
commit
b73aa8616b
37 files changed
+8711
No files matched your search
@@ -0,0 +1,78 @@
|
||||
'use server';
|
||||
|
||||
import { prisma } from '@/lib/db';
|
||||
import { verifyPassword } from '@/lib/auth';
|
||||
import { cookies } from 'next/headers';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
/**
|
||||
* Handle admin login.
|
||||
* @param formData - The form data containing the password.
|
||||
*/
|
||||
export async function loginAdmin(formData: FormData) {
|
||||
const password = formData.get('password') as string;
|
||||
|
||||
const config = await prisma.config.findUnique({
|
||||
where: { id: 'admin_password' },
|
||||
});
|
||||
|
||||
if (!config) {
|
||||
redirect('/setup');
|
||||
}
|
||||
|
||||
const isValid = await verifyPassword(password, config.value);
|
||||
|
||||
if (isValid) {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set('admin_auth', 'true', {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: 60 * 60 * 24, // 24 hours
|
||||
path: '/',
|
||||
});
|
||||
redirect('/admin');
|
||||
} else {
|
||||
throw new Error('Mot de passe incorrect.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout admin.
|
||||
*/
|
||||
export async function logoutAdmin() {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete('admin_auth');
|
||||
redirect('/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Is authenticated check.
|
||||
*/
|
||||
export async function isAuthenticated() {
|
||||
const cookieStore = await cookies();
|
||||
return cookieStore.has('admin_auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new menu button.
|
||||
*/
|
||||
export async function addButton(formData: FormData) {
|
||||
if (!(await isAuthenticated())) throw new Error('Non autorisé');
|
||||
|
||||
const name = formData.get('name') as string;
|
||||
const url = formData.get('url') as string;
|
||||
|
||||
if (!name || !url) throw new Error('Champs requis manquants.');
|
||||
|
||||
await prisma.button.create({
|
||||
data: { name, url, order: 0 },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a menu button.
|
||||
*/
|
||||
export async function deleteButton(id: number) {
|
||||
if (!(await isAuthenticated())) throw new Error('Non autorisé');
|
||||
await prisma.button.delete({ where: { id } });
|
||||
}
|
||||
Reference in new issue
Block a user