@20052507/dab-enterprise-bws 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,330 @@
1
+ # DAB Enterprise Ltd - Business Web Solution (BWS)
2
+ ## ERD and DFD Documentation
3
+
4
+ ---
5
+
6
+ ## Entity Relationship Diagram (ERD)
7
+
8
+ ### Entities and Attributes:
9
+
10
+ 1. **Product**
11
+ - ProductID (PK)
12
+ - ProductName
13
+ - Category
14
+ - Quantity
15
+ - UnitPrice
16
+ - TotalPrice (Computed: Quantity * UnitPrice)
17
+
18
+ 2. **Sales**
19
+ - SalesID (PK)
20
+ - ProductID (FK)
21
+ - SoldQuantity
22
+ - SoldUnitPrice
23
+ - SoldTotalPrice (Computed: SoldQuantity * SoldUnitPrice)
24
+ - SalesDate
25
+
26
+ 3. **StockStatus**
27
+ - StockID (PK)
28
+ - ProductID (FK)
29
+ - AvailableQuantity
30
+ - SoldQuantity
31
+ - RemainingQuantity (Computed: AvailableQuantity - SoldQuantity)
32
+
33
+ 4. **User** (for authentication)
34
+ - UserID (PK)
35
+ - Username
36
+ - Password (encrypted)
37
+ - CreatedAt
38
+
39
+ ### Relationships:
40
+
41
+ 1. **Product → Sales**: One-to-Many
42
+ - One Product can have many Sales records
43
+ - ProductID in Sales references ProductID in Product
44
+
45
+ 2. **Product → StockStatus**: One-to-One
46
+ - One Product has one StockStatus
47
+ - ProductID in StockStatus references ProductID in Product
48
+
49
+ ### ERD Representation:
50
+
51
+ ```
52
+ ┌─────────────────────┐
53
+ │ PRODUCT │
54
+ ├─────────────────────┤
55
+ │ ProductID (PK) │◄──────────┐
56
+ │ ProductName │ │
57
+ │ Category │ │
58
+ │ Quantity │ │
59
+ │ UnitPrice │ │
60
+ │ TotalPrice │ │
61
+ └─────────────────────┘ │
62
+ │ │
63
+ │ 1 │ 1
64
+ │ │
65
+ │ N │ 1
66
+ ▼ │
67
+ ┌─────────────────────┐ │
68
+ │ SALES │ │
69
+ ├─────────────────────┤ │
70
+ │ SalesID (PK) │ │
71
+ │ ProductID (FK) │ │
72
+ │ SoldQuantity │ │
73
+ │ SoldUnitPrice │ │
74
+ │ SoldTotalPrice │ │
75
+ │ SalesDate │ │
76
+ └─────────────────────┘ │
77
+
78
+ ┌─────────────────────┐ │
79
+ │ STOCKSTATUS │───────────┘
80
+ ├─────────────────────┤
81
+ │ StockID (PK) │
82
+ │ ProductID (FK) │
83
+ │ AvailableQuantity │
84
+ │ SoldQuantity │
85
+ │ RemainingQuantity │
86
+ └─────────────────────┘
87
+
88
+ ┌─────────────────────┐
89
+ │ USER │
90
+ ├─────────────────────┤
91
+ │ UserID (PK) │
92
+ │ Username │
93
+ │ Password │
94
+ │ CreatedAt │
95
+ └─────────────────────┘
96
+ ```
97
+
98
+ ### Cardinalities:
99
+ - Product to Sales: 1:N (One product can have multiple sales)
100
+ - Product to StockStatus: 1:1 (One product has one stock status)
101
+ - User: Independent entity for authentication
102
+
103
+ ---
104
+
105
+ ## Data Flow Diagram (DFD)
106
+
107
+ ### Level 0 DFD (Context Diagram):
108
+
109
+ ```
110
+ ┌─────────────────┐
111
+ │ USER │
112
+ └────────┬────────┘
113
+
114
+ │ 1. Login/Logout
115
+ │ 2. Input Product Data
116
+ │ 3. Input Sales Data
117
+ │ 4. View Reports
118
+
119
+ ┌─────────────────────────────────┐
120
+ │ DAB ENTERPRISE BWS SYSTEM │
121
+ │ (Business Web Solution) │
122
+ └─────────────────────────────────┘
123
+
124
+ │ 3. Display Reports
125
+ │ 4. Confirmation Messages
126
+
127
+ ┌─────────────────┐
128
+ │ DATABASE │
129
+ └─────────────────┘
130
+ ```
131
+
132
+ ### Level 1 DFD:
133
+
134
+ ```
135
+ ┌─────────────────┐
136
+ │ USER │
137
+ └────────┬────────┘
138
+
139
+ ├─────────────────────────────────────────┐
140
+ │ │
141
+ ▼ ▼
142
+ ┌─────────────────────┐ ┌─────────────────────┐
143
+ │ LOGIN PROCESS │ │ PRODUCT MANAGEMENT │
144
+ │ 1.0 │ │ 2.0 │
145
+ ├─────────────────────┤ ├─────────────────────┤
146
+ │ - Validate User │ │ - Add Product │
147
+ │ - Create Session │ │ - View Products │
148
+ │ - Encrypt Password │ │ - Update Product │
149
+ └─────────────────────┘ │ - Delete Product │
150
+ │ └─────────────────────┘
151
+ │ │
152
+ └─────────────────┬───────────────────────┘
153
+
154
+
155
+ ┌─────────────────────┐
156
+ │ DATA STORE │
157
+ │ (Database) │
158
+ └─────────────────────┘
159
+
160
+ ┌─────────────────┼─────────────────┐
161
+ │ │ │
162
+ ▼ ▼ ▼
163
+ ┌─────────────────────┐ ┌───────────────┐ ┌─────────────────┐
164
+ │ SALES MANAGEMENT │ │ STOCK STATUS │ │ REPORTS │
165
+ │ 3.0 │ │ 4.0 │ │ 5.0 │
166
+ ├─────────────────────┤ ├───────────────┤ ├─────────────────┤
167
+ │ - Add Sale │ │ - View Stock │ │ - Daily Sales │
168
+ │ - View Sales │ │ - Update Stock│ │ - Stock Status │
169
+ │ - Update Sale │ │ - Auto Calc │ │ - Export Report │
170
+ │ - Delete Sale │ │ │ │ │
171
+ └─────────────────────┘ └───────────────┘ └─────────────────┘
172
+ ```
173
+
174
+ ### Process Descriptions:
175
+
176
+ **Process 1.0 - Login Process**
177
+ - Input: Username, Password
178
+ - Process: Validate credentials, encrypt password, create session
179
+ - Output: Session token, access granted/denied
180
+
181
+ **Process 2.0 - Product Management**
182
+ - Input: Product details (Name, Category, Quantity, UnitPrice)
183
+ - Process: Calculate TotalPrice, validate data, store in database
184
+ - Output: Product confirmation, updated product list
185
+
186
+ **Process 3.0 - Sales Management**
187
+ - Input: Sales details (ProductID, SoldQuantity, SoldUnitPrice, SalesDate)
188
+ - Process: Calculate SoldTotalPrice, update stock, store sales record
189
+ - Output: Sales confirmation, updated stock status
190
+
191
+ **Process 4.0 - Stock Status**
192
+ - Input: ProductID
193
+ - Process: Calculate AvailableQuantity, SoldQuantity, RemainingQuantity
194
+ - Output: Current stock status
195
+
196
+ **Process 5.0 - Reports**
197
+ - Input: Date range, report type
198
+ - Process: Query database, aggregate data, format report
199
+ - Output: Daily sales report, Stock status report
200
+
201
+ ---
202
+
203
+ ## Database Schema
204
+
205
+ ### MySQL Tables:
206
+
207
+ ```sql
208
+ CREATE DATABASE db;
209
+ USE db;
210
+
211
+ -- Users Table
212
+ CREATE TABLE users (
213
+ UserID INT AUTO_INCREMENT PRIMARY KEY,
214
+ Username VARCHAR(50) UNIQUE NOT NULL,
215
+ Password VARCHAR(255) NOT NULL,
216
+ CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
217
+ );
218
+
219
+ -- Products Table
220
+ CREATE TABLE products (
221
+ ProductID INT AUTO_INCREMENT PRIMARY KEY,
222
+ ProductName VARCHAR(100) NOT NULL,
223
+ Category VARCHAR(50) NOT NULL,
224
+ Quantity INT NOT NULL DEFAULT 0,
225
+ UnitPrice DECIMAL(10, 2) NOT NULL,
226
+ TotalPrice DECIMAL(10, 2) GENERATED ALWAYS AS (Quantity * UnitPrice) STORED,
227
+ CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
228
+ );
229
+
230
+ -- Sales Table
231
+ CREATE TABLE sales (
232
+ SalesID INT AUTO_INCREMENT PRIMARY KEY,
233
+ ProductID INT NOT NULL,
234
+ SoldQuantity INT NOT NULL,
235
+ SoldUnitPrice DECIMAL(10, 2) NOT NULL,
236
+ SoldTotalPrice DECIMAL(10, 2) GENERATED ALWAYS AS (SoldQuantity * SoldUnitPrice) STORED,
237
+ SalesDate DATE NOT NULL,
238
+ CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
239
+ FOREIGN KEY (ProductID) REFERENCES products(ProductID) ON DELETE CASCADE
240
+ );
241
+
242
+ -- StockStatus Table
243
+ CREATE TABLE stockstatus (
244
+ StockID INT AUTO_INCREMENT PRIMARY KEY,
245
+ ProductID INT UNIQUE NOT NULL,
246
+ AvailableQuantity INT NOT NULL DEFAULT 0,
247
+ SoldQuantity INT NOT NULL DEFAULT 0,
248
+ RemainingQuantity INT GENERATED ALWAYS AS (AvailableQuantity - SoldQuantity) STORED,
249
+ LastUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
250
+ FOREIGN KEY (ProductID) REFERENCES products(ProductID) ON DELETE CASCADE
251
+ );
252
+ ```
253
+
254
+ ---
255
+
256
+ ## Technology Stack
257
+
258
+ ### Backend:
259
+ - Node.js (JavaScript Runtime)
260
+ - Express.js (Web Framework)
261
+ - MySQL (Database)
262
+ - bcryptjs (Password Encryption)
263
+ - express-session (Session Management)
264
+ - cors (Cross-Origin Resource Sharing)
265
+ - mysql2 (MySQL Driver)
266
+
267
+ ### Frontend:
268
+ - React.js (UI Framework)
269
+ - Tailwind CSS (Styling)
270
+ - Axios (HTTP Client)
271
+ - React Router (Navigation)
272
+
273
+ ---
274
+
275
+ ## Project Structure
276
+
277
+ ```
278
+ FirstName_LastName_National_Practical_Exam_2026/
279
+ ├── backend-project/
280
+ │ ├── config/
281
+ │ │ └── database.js
282
+ │ ├── controllers/
283
+ │ │ ├── authController.js
284
+ │ │ ├── productController.js
285
+ │ │ ├── salesController.js
286
+ │ │ └── stockController.js
287
+ │ ├── middleware/
288
+ │ │ └── authMiddleware.js
289
+ │ ├── routes/
290
+ │ │ ├── authRoutes.js
291
+ │ │ ├── productRoutes.js
292
+ │ │ ├── salesRoutes.js
293
+ │ │ └── stockRoutes.js
294
+ │ ├── server.js
295
+ │ └── package.json
296
+ ├── frontend-project/
297
+ │ ├── public/
298
+ │ ├── src/
299
+ │ │ ├── components/
300
+ │ │ │ ├── Login.js
301
+ │ │ │ ├── ProductForm.js
302
+ │ │ │ ├── SalesForm.js
303
+ │ │ │ ├── StockStatus.js
304
+ │ │ │ ├── Reports.js
305
+ │ │ │ └── Navbar.js
306
+ │ │ ├── pages/
307
+ │ │ │ ├── Products.js
308
+ │ │ │ ├── Sales.js
309
+ │ │ │ ├── Stock.js
310
+ │ │ │ └── Dashboard.js
311
+ │ │ ├── App.js
312
+ │ │ └── index.js
313
+ │ └── package.json
314
+ └── ERD_DFD_Documentation.md
315
+ ```
316
+
317
+ ---
318
+
319
+ ## Implementation Notes
320
+
321
+ 1. **Password Security**: Using bcryptjs for password hashing with salt rounds of 10
322
+ 2. **Session Management**: Using express-session with secure cookie settings
323
+ 3. **CRUD Operations**:
324
+ - Products: Insert, Delete, Update, Retrieve (all operations)
325
+ - Sales: Insert only
326
+ - StockStatus: Insert only (auto-calculated)
327
+ 4. **Reports**:
328
+ - Daily sales report: Aggregates sales by date
329
+ - Stock status report: Shows all products with current stock levels
330
+ 5. **Responsive Design**: Using Tailwind CSS for mobile-first responsive UI
package/README.md ADDED
@@ -0,0 +1,307 @@
1
+ # DAB Enterprise Ltd - Business Web Solution (BWS)
2
+
3
+ A comprehensive full-stack web application for managing products, sales, and inventory for DAB Enterprise Ltd located in Kigali City, Rwanda.
4
+
5
+ ## Project Overview
6
+
7
+ This project addresses the inefficiencies of the manual paper-based system by providing a modern web-based solution for:
8
+ - Product management
9
+ - Sales tracking
10
+ - Stock status monitoring
11
+ - Report generation
12
+ - User authentication
13
+
14
+ ## Technology Stack
15
+
16
+ ### Backend
17
+ - **Node.js** - JavaScript runtime
18
+ - **Express.js** - Web framework
19
+ - **MySQL** - Database
20
+ - **bcryptjs** - Password encryption
21
+ - **express-session** - Session management
22
+ - **cors** - Cross-Origin Resource Sharing
23
+ - **mysql2** - MySQL driver
24
+
25
+ ### Frontend
26
+ - **React.js** - UI framework
27
+ - **Tailwind CSS** - Styling
28
+ - **Axios** - HTTP client
29
+ - **React Router** - Navigation
30
+
31
+ ## Project Structure
32
+
33
+ ```
34
+ FirstName_LastName_National_Practical_Exam_2026/
35
+ ├── backend-project/
36
+ │ ├── config/
37
+ │ │ └── database.js
38
+ │ ├── controllers/
39
+ │ │ ├── authController.js
40
+ │ │ ├── productController.js
41
+ │ │ ├── salesController.js
42
+ │ │ └── stockController.js
43
+ │ ├── middleware/
44
+ │ │ └── authMiddleware.js
45
+ │ ├── routes/
46
+ │ │ ├── authRoutes.js
47
+ │ │ ├── productRoutes.js
48
+ │ │ ├── salesRoutes.js
49
+ │ │ └── stockRoutes.js
50
+ │ ├── server.js
51
+ │ └── package.json
52
+ ├── frontend-project/
53
+ │ ├── public/
54
+ │ │ └── index.html
55
+ │ ├── src/
56
+ │ │ ├── components/
57
+ │ │ │ └── Navbar.js
58
+ │ │ ├── pages/
59
+ │ │ │ ├── Login.js
60
+ │ │ │ ├── Dashboard.js
61
+ │ │ │ ├── Products.js
62
+ │ │ │ ├── Sales.js
63
+ │ │ │ ├── Stock.js
64
+ │ │ │ └── Reports.js
65
+ │ │ ├── App.js
66
+ │ │ ├── index.js
67
+ │ │ └── index.css
68
+ │ ├── tailwind.config.js
69
+ │ ├── postcss.config.js
70
+ │ └── package.json
71
+ ├── ERD_DFD_Documentation.md
72
+ └── README.md
73
+ ```
74
+
75
+ ## Prerequisites
76
+
77
+ Before running this application, ensure you have the following installed:
78
+
79
+ 1. **Node.js** (v14 or higher) - [Download here](https://nodejs.org/)
80
+ 2. **MySQL Server** (v5.7 or higher) - [Download here](https://dev.mysql.com/downloads/mysql/)
81
+ 3. **npm** (comes with Node.js)
82
+
83
+ ## Installation Instructions
84
+
85
+ ### 1. Database Setup
86
+
87
+ The application automatically creates the database and tables on first run. However, ensure MySQL is running with the following default configuration:
88
+
89
+ - **Host**: localhost
90
+ - **User**: root
91
+ - **Password**: (empty)
92
+
93
+ If your MySQL has a different password, update the configuration in `backend-project/config/database.js` and `backend-project/server.js`.
94
+
95
+ ### 2. Backend Setup
96
+
97
+ Navigate to the backend project directory:
98
+
99
+ ```bash
100
+ cd backend-project
101
+ ```
102
+
103
+ Install dependencies:
104
+
105
+ ```bash
106
+ npm install
107
+ ```
108
+
109
+ Start the backend server:
110
+
111
+ ```bash
112
+ npm start
113
+ ```
114
+
115
+ The backend will run on `http://localhost:5000`
116
+
117
+ ### 3. Frontend Setup
118
+
119
+ Navigate to the frontend project directory:
120
+
121
+ ```bash
122
+ cd frontend-project
123
+ ```
124
+
125
+ Install dependencies:
126
+
127
+ ```bash
128
+ npm install
129
+ ```
130
+
131
+ Start the frontend development server:
132
+
133
+ ```bash
134
+ npm start
135
+ ```
136
+
137
+ The frontend will run on `http://localhost:3000`
138
+
139
+ ## Default Login Credentials
140
+
141
+ The application creates a default admin user on first run:
142
+
143
+ - **Username**: admin
144
+ - **Password**: Admin@123
145
+
146
+ **Important**: Change this password after first login for security.
147
+
148
+ ## Features
149
+
150
+ ### 1. Authentication
151
+ - Session-based login system
152
+ - Password encryption using bcrypt
153
+ - Secure session management
154
+
155
+ ### 2. Product Management
156
+ - Add new products
157
+ - View all products
158
+ - Edit product details
159
+ - Delete products
160
+ - Automatic total price calculation
161
+
162
+ ### 3. Sales Management
163
+ - Record new sales
164
+ - View sales history
165
+ - Edit sales records
166
+ - Delete sales records
167
+ - Automatic stock update on sale
168
+
169
+ ### 4. Stock Status
170
+ - View current stock levels
171
+ - Real-time stock monitoring
172
+ - Low stock alerts
173
+ - Out of stock indicators
174
+
175
+ ### 5. Reports
176
+ - **Daily Sales Report**: View sales summary by date
177
+ - **Stock Status Report**: Complete inventory overview
178
+ - Print functionality for reports
179
+
180
+ ## API Endpoints
181
+
182
+ ### Authentication
183
+ - `POST /api/auth/login` - User login
184
+ - `POST /api/auth/logout` - User logout
185
+ - `GET /api/auth/check` - Check authentication status
186
+
187
+ ### Products
188
+ - `GET /api/products` - Get all products
189
+ - `GET /api/products/:id` - Get product by ID
190
+ - `POST /api/products` - Create new product
191
+ - `PUT /api/products/:id` - Update product
192
+ - `DELETE /api/products/:id` - Delete product
193
+
194
+ ### Sales
195
+ - `GET /api/sales` - Get all sales
196
+ - `GET /api/sales/:id` - Get sale by ID
197
+ - `POST /api/sales` - Create new sale
198
+ - `PUT /api/sales/:id` - Update sale
199
+ - `DELETE /api/sales/:id` - Delete sale
200
+ - `GET /api/sales/report/daily` - Get daily sales report
201
+ - `GET /api/sales/report/daily/details` - Get daily sales details
202
+
203
+ ### Stock Status
204
+ - `GET /api/stock` - Get all stock status
205
+ - `GET /api/stock/:id` - Get stock status by product ID
206
+ - `GET /api/stock/report/status` - Get stock status report
207
+
208
+ ## Database Schema
209
+
210
+ ### Users Table
211
+ - UserID (PK)
212
+ - Username
213
+ - Password (encrypted)
214
+ - CreatedAt
215
+
216
+ ### Products Table
217
+ - ProductID (PK)
218
+ - ProductName
219
+ - Category
220
+ - Quantity
221
+ - UnitPrice
222
+ - TotalPrice (computed)
223
+ - CreatedAt
224
+
225
+ ### Sales Table
226
+ - SalesID (PK)
227
+ - ProductID (FK)
228
+ - SoldQuantity
229
+ - SoldUnitPrice
230
+ - SoldTotalPrice (computed)
231
+ - SalesDate
232
+ - CreatedAt
233
+
234
+ ### StockStatus Table
235
+ - StockID (PK)
236
+ - ProductID (FK)
237
+ - AvailableQuantity
238
+ - SoldQuantity
239
+ - RemainingQuantity (computed)
240
+ - LastUpdated
241
+
242
+ ## ERD and DFD
243
+
244
+ Detailed Entity Relationship Diagram (ERD) and Data Flow Diagram (DFD) are available in `ERD_DFD_Documentation.md`.
245
+
246
+ ## Usage
247
+
248
+ 1. Start the MySQL server
249
+ 2. Start the backend server (`cd backend-project && npm start`)
250
+ 3. Start the frontend server (`cd frontend-project && npm start`)
251
+ 4. Open `http://localhost:3000` in your browser
252
+ 5. Login with default credentials (admin / Admin@123)
253
+ 6. Navigate through the application using the menu bar
254
+
255
+ ## Security Features
256
+
257
+ - Password encryption using bcrypt (10 salt rounds)
258
+ - Session-based authentication
259
+ - Protected API routes
260
+ - CORS configuration
261
+ - SQL injection prevention (parameterized queries)
262
+
263
+ ## Responsive Design
264
+
265
+ The application is fully responsive and works on:
266
+ - Desktop computers
267
+ - Tablets
268
+ - Mobile devices
269
+
270
+ ## Troubleshooting
271
+
272
+ ### Backend won't start
273
+ - Ensure MySQL is running
274
+ - Check database credentials in `config/database.js`
275
+ - Verify port 5000 is not in use
276
+
277
+ ### Frontend won't start
278
+ - Ensure Node.js is installed
279
+ - Check that dependencies are installed
280
+ - Verify port 3000 is not in use
281
+
282
+ ### Database connection error
283
+ - Verify MySQL server is running
284
+ - Check username and password in database config
285
+ - Ensure database `dab_enterprise_db` can be created
286
+
287
+ ## Project Removal
288
+
289
+ After the assessment, to permanently remove the project:
290
+
291
+ 1. Stop both backend and frontend servers
292
+ 2. Delete the project folder: `FirstName_LastName_National_Practical_Exam_2026`
293
+ 3. Optionally, drop the database: `DROP DATABASE dab_enterprise_db;`
294
+
295
+ **Note**: Always ask permission from the assessor before removing the project.
296
+
297
+ ## License
298
+
299
+ This project was created for educational assessment purposes.
300
+
301
+ ## Contact
302
+
303
+ For questions or issues related to this project, please contact the development team.
304
+
305
+ ---
306
+
307
+ **Project completed for DAB Enterprise Ltd - National Practical Exam 2026**
@@ -0,0 +1,13 @@
1
+ const mysql = require('mysql2/promise');
2
+
3
+ const pool = mysql.createPool({
4
+ host: 'localhost',
5
+ user: 'root',
6
+ password: '',
7
+ database: 'db',
8
+ waitForConnections: true,
9
+ connectionLimit: 10,
10
+ queueLimit: 0
11
+ });
12
+
13
+ module.exports = pool;
@@ -0,0 +1,67 @@
1
+ const pool = require('../config/database');
2
+ const bcrypt = require('bcryptjs');
3
+
4
+ const login = async (req, res) => {
5
+ try {
6
+ const { username, password } = req.body;
7
+
8
+ if (!username || !password) {
9
+ return res.status(400).json({ message: 'Username and password are required' });
10
+ }
11
+
12
+ const [users] = await pool.query(
13
+ 'SELECT * FROM users WHERE Username = ?',
14
+ [username]
15
+ );
16
+
17
+ if (users.length === 0) {
18
+ return res.status(401).json({ message: 'Invalid credentials' });
19
+ }
20
+
21
+ const user = users[0];
22
+ const isPasswordValid = await bcrypt.compare(password, user.Password);
23
+
24
+ if (!isPasswordValid) {
25
+ return res.status(401).json({ message: 'Invalid credentials' });
26
+ }
27
+
28
+ req.session.userId = user.UserID;
29
+ req.session.username = user.Username;
30
+
31
+ res.json({
32
+ message: 'Login successful',
33
+ user: {
34
+ userId: user.UserID,
35
+ username: user.Username
36
+ }
37
+ });
38
+ } catch (error) {
39
+ console.error('Login error:', error);
40
+ res.status(500).json({ message: 'Server error' });
41
+ }
42
+ };
43
+
44
+ const logout = (req, res) => {
45
+ req.session.destroy((err) => {
46
+ if (err) {
47
+ return res.status(500).json({ message: 'Logout failed' });
48
+ }
49
+ res.json({ message: 'Logout successful' });
50
+ });
51
+ };
52
+
53
+ const checkAuth = (req, res) => {
54
+ if (req.session && req.session.userId) {
55
+ res.json({
56
+ authenticated: true,
57
+ user: {
58
+ userId: req.session.userId,
59
+ username: req.session.username
60
+ }
61
+ });
62
+ } else {
63
+ res.json({ authenticated: false });
64
+ }
65
+ };
66
+
67
+ module.exports = { login, logout, checkAuth };