create a comprehensive admin dashbioard with permalink /admin/ and creat
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user