create a comprehensive admin dashbioard with permalink /admin/ and creat

This commit is contained in:
Leon Serfaty G
2025-07-17 10:40:28 +00:00
parent d6e45f10e4
commit a1f3e2b3f5
5 changed files with 246 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
'use server';
import { redirect } from 'next/navigation';
import { cookies } from 'next/headers';
const FAKE_USER = {
email: 'admin@example.com',
password: 'password',
name: 'Admin User',
};
export async function signIn(formData: FormData) {
const email = formData.get('email');
const password = formData.get('password');
if (email === FAKE_USER.email && password === FAKE_USER.password) {
const sessionData = {
isLoggedIn: true,
email: FAKE_USER.email,
name: FAKE_USER.name,
};
cookies().set('session', JSON.stringify(sessionData), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60 * 24 * 7, // One week
path: '/',
});
redirect('/admin');
}
// In a real app, you'd handle the error case, e.g., redirect to login with an error message.
// For simplicity, we'll just redirect back.
redirect('/login');
}
export async function signOut() {
cookies().delete('session');
redirect('/login');
}
export async function getSession() {
const sessionCookie = cookies().get('session');
if (!sessionCookie) {
return null;
}
try {
return JSON.parse(sessionCookie.value);
} catch (error) {
console.error('Failed to parse session cookie:', error);
return null;
}
}