updateweb
This commit is contained in:
343
old-docs/ADMIN_NAVIGATION_SESSION_FIX.md
Normal file
343
old-docs/ADMIN_NAVIGATION_SESSION_FIX.md
Normal file
@@ -0,0 +1,343 @@
|
||||
# Admin Panel Navigation & Session Management Fix
|
||||
|
||||
## Problem
|
||||
|
||||
When clicking on navigation items in the admin panel's left sidebar or live tiles, users were being signed out or redirected to the login page.
|
||||
|
||||
## Root Cause
|
||||
|
||||
1. Each admin page had its own `checkAuth()` function making redundant API calls
|
||||
2. Multiple simultaneous authentication checks could interfere with session management
|
||||
3. No centralized authentication handling across admin pages
|
||||
4. Missing public API routes for frontend to consume published content
|
||||
|
||||
## Solution Implemented
|
||||
|
||||
### 1. Centralized Authentication (`/admin/js/auth.js`)
|
||||
|
||||
Created a shared authentication utility that:
|
||||
|
||||
- Provides a single `checkAuth()` function used by all admin pages
|
||||
- Handles session validation with `/api/admin/session` endpoint
|
||||
- Manages authentication state globally via `window.adminAuth`
|
||||
- Provides shared `logout()`, `showSuccess()`, and `showError()` functions
|
||||
- Automatically checks authentication on page load (except login page)
|
||||
|
||||
### 2. Updated All Admin Pages
|
||||
|
||||
Modified all HTML pages to include the shared `auth.js` script:
|
||||
|
||||
- ✅ `/admin/dashboard.html`
|
||||
- ✅ `/admin/homepage.html`
|
||||
- ✅ `/admin/products.html`
|
||||
- ✅ `/admin/portfolio.html`
|
||||
- ✅ `/admin/blog.html`
|
||||
- ✅ `/admin/pages.html`
|
||||
- ✅ `/admin/menu.html`
|
||||
- ✅ `/admin/settings.html`
|
||||
- ✅ `/admin/users.html`
|
||||
|
||||
### 3. Updated JavaScript Files
|
||||
|
||||
Removed duplicate `checkAuth()` functions from individual JS files and updated to use the shared version:
|
||||
|
||||
- ✅ `products.js` - Product management
|
||||
- ✅ `homepage.js` - Homepage editor
|
||||
- ✅ `blog.js` - Blog post management
|
||||
- ✅ `portfolio.js` - Portfolio project management
|
||||
- ✅ `pages.js` - Custom pages management
|
||||
- ✅ `settings.js` - Site settings
|
||||
- ✅ `users.js` - User management
|
||||
- ✅ `menu.html` (inline script) - Menu management
|
||||
|
||||
### 4. Enhanced Backend Routes
|
||||
|
||||
#### Admin Routes (`/api/admin/*`)
|
||||
|
||||
All routes require authentication via `requireAuth` middleware:
|
||||
|
||||
**Products:**
|
||||
|
||||
- `GET /api/admin/products` - List all products
|
||||
- `GET /api/admin/products/:id` - Get single product
|
||||
- `POST /api/admin/products` - Create product
|
||||
- `PUT /api/admin/products/:id` - Update product
|
||||
- `DELETE /api/admin/products/:id` - Delete product
|
||||
|
||||
**Portfolio:**
|
||||
|
||||
- `GET /api/admin/portfolio/projects` - List all projects
|
||||
- `GET /api/admin/portfolio/projects/:id` - Get single project
|
||||
- `POST /api/admin/portfolio/projects` - Create project
|
||||
- `PUT /api/admin/portfolio/projects/:id` - Update project
|
||||
- `DELETE /api/admin/portfolio/projects/:id` - Delete project
|
||||
|
||||
**Blog:**
|
||||
|
||||
- `GET /api/admin/blog` - List all blog posts
|
||||
- `GET /api/admin/blog/:id` - Get single post
|
||||
- `POST /api/admin/blog` - Create blog post
|
||||
- `PUT /api/admin/blog/:id` - Update blog post
|
||||
- `DELETE /api/admin/blog/:id` - Delete blog post
|
||||
|
||||
**Pages:**
|
||||
|
||||
- `GET /api/admin/pages` - List all custom pages
|
||||
- `GET /api/admin/pages/:id` - Get single page
|
||||
- `POST /api/admin/pages` - Create page
|
||||
- `PUT /api/admin/pages/:id` - Update page
|
||||
- `DELETE /api/admin/pages/:id` - Delete page
|
||||
|
||||
**Homepage:**
|
||||
|
||||
- `GET /api/admin/homepage/settings` - Get homepage settings
|
||||
- `POST /api/admin/homepage/settings` - Save homepage settings
|
||||
|
||||
**Menu:**
|
||||
|
||||
- `GET /api/admin/menu` - Get menu items
|
||||
- `POST /api/admin/menu` - Save menu structure
|
||||
|
||||
**Settings:**
|
||||
|
||||
- `GET /api/admin/settings` - Get site settings
|
||||
- `POST /api/admin/settings` - Save site settings
|
||||
|
||||
**Dashboard:**
|
||||
|
||||
- `GET /api/admin/dashboard/stats` - Get dashboard statistics
|
||||
|
||||
#### Public Routes (`/api/*`)
|
||||
|
||||
Added/enhanced routes for frontend consumption (no authentication required):
|
||||
|
||||
**Products:**
|
||||
|
||||
- `GET /api/products` - List active products
|
||||
- `GET /api/products/featured` - Get featured products
|
||||
- `GET /api/products/:id` - Get single product
|
||||
|
||||
**Portfolio:**
|
||||
|
||||
- `GET /api/portfolio/projects` - List active projects
|
||||
|
||||
**Blog:**
|
||||
|
||||
- `GET /api/blog/posts` - List published posts
|
||||
- `GET /api/blog/posts/:slug` - Get single post by slug
|
||||
|
||||
**Pages:**
|
||||
|
||||
- `GET /api/pages` - List published custom pages
|
||||
- `GET /api/pages/:slug` - Get single page by slug
|
||||
|
||||
**Menu:**
|
||||
|
||||
- `GET /api/menu` - Get visible menu items
|
||||
|
||||
**Homepage:**
|
||||
|
||||
- `GET /api/homepage/settings` - Get homepage configuration
|
||||
- `GET /api/homepage/sections` - Get homepage sections
|
||||
|
||||
**Settings:**
|
||||
|
||||
- `GET /api/settings` - Get public site settings
|
||||
|
||||
## Session Configuration
|
||||
|
||||
The backend uses PostgreSQL session storage with these settings:
|
||||
|
||||
```javascript
|
||||
{
|
||||
secret: process.env.SESSION_SECRET || "skyart-shop-secret-2025",
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
httpOnly: true,
|
||||
maxAge: 24 hours,
|
||||
sameSite: "lax"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
### Run the Test Script
|
||||
|
||||
```bash
|
||||
cd /media/pts/Website/SkyArtShop/backend
|
||||
./test-navigation.sh
|
||||
```
|
||||
|
||||
### Manual Testing Steps
|
||||
|
||||
1. **Login Test:**
|
||||
- Navigate to `http://localhost:5000/admin/login.html`
|
||||
- Login with your credentials
|
||||
- Verify successful redirect to dashboard
|
||||
|
||||
2. **Navigation Test:**
|
||||
- Click each item in the left sidebar
|
||||
- Verify you remain logged in
|
||||
- Verify each page loads correctly with its data
|
||||
|
||||
3. **Content Creation Test:**
|
||||
- Navigate to Products section
|
||||
- Click "Add New Product"
|
||||
- Fill in product details
|
||||
- Click "Save & Publish"
|
||||
- Verify product appears in the list
|
||||
|
||||
4. **Frontend Publishing Test:**
|
||||
- Create/edit content in admin panel
|
||||
- Mark it as "Published" or "Active"
|
||||
- View the public API endpoint (e.g., `/api/products`)
|
||||
- Verify the content appears
|
||||
|
||||
5. **Session Persistence Test:**
|
||||
- Login to admin panel
|
||||
- Navigate through multiple sections
|
||||
- Leave browser open for several minutes
|
||||
- Continue navigating
|
||||
- Verify session remains active for 24 hours
|
||||
|
||||
## How Content Publishing Works
|
||||
|
||||
### From Admin to Frontend Flow
|
||||
|
||||
1. **Create Content in Admin Panel:**
|
||||
- Login to `/admin/`
|
||||
- Navigate to any section (Products, Blog, Portfolio, etc.)
|
||||
- Click "Create" or "Add New"
|
||||
- Fill in details
|
||||
- Enable "Active" or "Published" toggle
|
||||
- Click "Save & Publish"
|
||||
|
||||
2. **Content Stored in Database:**
|
||||
- Data saved to PostgreSQL with `isactive=true` or `ispublished=true`
|
||||
- Timestamps recorded (createdat, updatedat)
|
||||
|
||||
3. **Frontend Accesses via Public API:**
|
||||
- Frontend JavaScript calls public endpoints (e.g., `/api/products`)
|
||||
- Backend filters for only active/published content
|
||||
- JSON data returned to frontend
|
||||
- Frontend renders the content dynamically
|
||||
|
||||
### Example Flow - Adding a Product
|
||||
|
||||
**Admin Panel:**
|
||||
|
||||
```
|
||||
1. Login → Dashboard → Products
|
||||
2. Click "Add New Product"
|
||||
3. Enter: Name, Price, Description, Image
|
||||
4. Toggle "Active" to ON
|
||||
5. Click "Save & Publish"
|
||||
6. Backend: POST /api/admin/products
|
||||
7. Product saved with isactive=true
|
||||
```
|
||||
|
||||
**Frontend:**
|
||||
|
||||
```
|
||||
1. Shop page loads
|
||||
2. JavaScript: fetch('/api/products')
|
||||
3. Backend: Returns only products where isactive=true
|
||||
4. Frontend: Renders product cards with data
|
||||
5. Customer sees the new product
|
||||
```
|
||||
|
||||
## Files Changed
|
||||
|
||||
### Created
|
||||
|
||||
- `/website/admin/js/auth.js` - Shared authentication utility
|
||||
- `/backend/test-navigation.sh` - Navigation test script
|
||||
|
||||
### Modified
|
||||
|
||||
- `/backend/routes/public.js` - Added public API routes for pages, menu, blog posts by slug, pages by slug, homepage settings, menu items
|
||||
- `/website/admin/dashboard.html` - Added auth.js script
|
||||
- `/website/admin/homepage.html` - Added auth.js script
|
||||
- `/website/admin/products.html` - Added auth.js script
|
||||
- `/website/admin/portfolio.html` - Added auth.js script
|
||||
- `/website/admin/blog.html` - Added auth.js script
|
||||
- `/website/admin/pages.html` - Added auth.js script
|
||||
- `/website/admin/menu.html` - Added auth.js script, updated inline checkAuth
|
||||
- `/website/admin/settings.html` - Added auth.js script
|
||||
- `/website/admin/users.html` - Added auth.js script
|
||||
- `/website/admin/js/products.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/homepage.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/blog.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/portfolio.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/pages.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/settings.js` - Removed duplicate checkAuth
|
||||
- `/website/admin/js/users.js` - Removed duplicate checkAuth
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Issue: Still getting logged out
|
||||
|
||||
**Solution:**
|
||||
|
||||
- Clear browser cookies and cache
|
||||
- Verify SESSION_SECRET is set in `.env`
|
||||
- Check PostgreSQL session table exists
|
||||
- Restart backend server
|
||||
|
||||
### Issue: Content not appearing on frontend
|
||||
|
||||
**Solution:**
|
||||
|
||||
- Verify content is marked as "Active" or "Published" in admin panel
|
||||
- Check browser console for API errors
|
||||
- Verify public routes are accessible (test with curl or browser)
|
||||
- Check database records have `isactive=true` or `ispublished=true`
|
||||
|
||||
### Issue: 401 Unauthorized errors
|
||||
|
||||
**Solution:**
|
||||
|
||||
- Verify you're logged in
|
||||
- Check session cookie is being sent (browser DevTools → Network → Headers)
|
||||
- Verify backend session store is working (check session table in database)
|
||||
- Try logging out and back in
|
||||
|
||||
### Issue: Navigation not working
|
||||
|
||||
**Solution:**
|
||||
|
||||
- Verify all admin HTML files have `<script src="/admin/js/auth.js"></script>`
|
||||
- Check browser console for JavaScript errors
|
||||
- Verify auth.js is accessible at `/admin/js/auth.js`
|
||||
- Clear browser cache
|
||||
|
||||
## Benefits of This Implementation
|
||||
|
||||
1. **✅ Consistent Authentication:** All pages use the same authentication logic
|
||||
2. **✅ Better Session Management:** No conflicting authentication checks
|
||||
3. **✅ Centralized Error Handling:** Uniform error messages and redirects
|
||||
4. **✅ Easier Maintenance:** Update auth logic in one place
|
||||
5. **✅ Complete API Coverage:** Full CRUD operations for all content types
|
||||
6. **✅ Frontend Integration:** Public APIs ready for frontend consumption
|
||||
7. **✅ Better UX:** Seamless navigation without unwanted logouts
|
||||
8. **✅ Scalable:** Easy to add new admin pages or features
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. Test all navigation links thoroughly
|
||||
2. Create sample content in each section
|
||||
3. Verify content appears on frontend
|
||||
4. Set up proper error logging for production
|
||||
5. Consider adding activity logging for admin actions
|
||||
6. Implement role-based permissions for different user types
|
||||
7. Add image upload functionality for products, blog, portfolio
|
||||
8. Set up automated backups of database content
|
||||
|
||||
---
|
||||
|
||||
**Last Updated:** December 13, 2025
|
||||
**Backend Version:** 1.0.0
|
||||
**Status:** ✅ Fully Operational
|
||||
Reference in New Issue
Block a user