-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
181 lines (155 loc) · 6.17 KB
/
Copy pathserver.js
File metadata and controls
181 lines (155 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const path = require("path");
const XLSX = require("xlsx");
const cors = require("cors");
const bcrypt = require("bcryptjs");
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Path to Excel file
const filePath = path.join(__dirname, "registrations.xlsx");
function readWorkbook() {
if (fs.existsSync(filePath)) {
return XLSX.readFile(filePath);
}
return XLSX.utils.book_new();
}
function readSheet(sheetName) {
const workbook = readWorkbook();
if (!workbook.SheetNames.includes(sheetName)) {
return [];
}
return XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
}
function saveToExcel(sheetName, data) {
let workbook;
if (fs.existsSync(filePath)) {
workbook = XLSX.readFile(filePath);
} else {
workbook = XLSX.utils.book_new();
}
// Check if sheet already exists
let worksheet;
if (workbook.SheetNames.includes(sheetName)) {
worksheet = workbook.Sheets[sheetName];
let existingData = XLSX.utils.sheet_to_json(worksheet);
existingData.push(data);
worksheet = XLSX.utils.json_to_sheet(existingData);
} else {
worksheet = XLSX.utils.json_to_sheet([data]);
}
// Append/Update sheet
workbook.Sheets[sheetName] = worksheet;
if (!workbook.SheetNames.includes(sheetName)) {
XLSX.utils.book_append_sheet(workbook, worksheet, sheetName);
}
// Save workbook
XLSX.writeFile(workbook, filePath);
}
function normalizeEmail(value) {
return String(value || "").trim().toLowerCase();
}
function getField(row, fieldNames) {
for (const fieldName of fieldNames) {
if (row[fieldName] !== undefined) {
return row[fieldName];
}
}
return "";
}
// ------------------- ROUTES --------------------
app.get("/api/test", (req, res) => {
res.json({ success: true, message: "Server is running" });
});
app.post("/api/register", async (req, res) => {
try {
const requiredFields = ["name", "email", "password", "phone", "age", "gender", "weight", "goal", "domain", "program"];
for (const field of requiredFields) {
if (!req.body[field] || String(req.body[field]).trim() === "") {
return res.status(400).json({ error: `Missing required field: ${field}` });
}
}
const email = normalizeEmail(req.body.email);
const users = readSheet("Users");
const alreadyExists = users.some((user) =>
normalizeEmail(getField(user, ["email", "Email"])) === email
);
if (alreadyExists) {
return res.status(400).json({ error: "Email already registered" });
}
const passwordHash = await bcrypt.hash(String(req.body.password), 10);
const userToSave = {
name: String(req.body.name).trim(),
email,
passwordHash,
phone: String(req.body.phone).trim(),
age: String(req.body.age).trim(),
gender: String(req.body.gender).trim(),
weight: String(req.body.weight).trim(),
goal: String(req.body.goal).trim(),
domain: String(req.body.domain).trim(),
program: String(req.body.program).trim(),
createdAt: new Date().toISOString()
};
saveToExcel("Users", userToSave);
const { passwordHash: _ignored, ...safeUser } = userToSave;
res.status(201).json({ success: true, message: "Registration successful", user: safeUser });
} catch (error) {
res.status(500).json({ error: "Unable to register user" });
}
});
app.post("/api/login", async (req, res) => {
try {
const email = normalizeEmail(req.body.email);
const password = String(req.body.password || "");
if (!email || !password) {
return res.status(400).json({ error: "Email and password are required" });
}
const users = readSheet("Users");
const user = users.find((row) => normalizeEmail(getField(row, ["email", "Email"])) === email);
if (!user) {
return res.status(401).json({ error: "Invalid email or password" });
}
const storedHash = String(getField(user, ["passwordHash", "PasswordHash", "password", "Password"]));
const isValidPassword = storedHash ? await bcrypt.compare(password, storedHash) : false;
if (!isValidPassword) {
return res.status(401).json({ error: "Invalid email or password" });
}
const safeUser = {
name: getField(user, ["name", "Name"]) || "",
email: getField(user, ["email", "Email"]) || "",
phone: getField(user, ["phone", "Phone"]) || "",
age: getField(user, ["age", "Age"]) || "",
gender: getField(user, ["gender", "Gender"]) || "",
weight: getField(user, ["weight", "Weight"]) || "",
goal: getField(user, ["goal", "Goal"]) || "",
domain: getField(user, ["domain", "Domain"]) || "",
program: getField(user, ["program", "Program"]) || ""
};
res.json({ success: true, message: "Login successful", user: safeUser });
} catch (error) {
res.status(500).json({ error: "Unable to login" });
}
});
// Signup
app.post("/api/signup", (req, res) => {
saveToExcel("Signup", req.body);
res.json({ success: true, message: "Signup saved successfully!" });
});
// Live Class Registration
app.post("/api/live", (req, res) => {
saveToExcel("Live Classes", req.body);
res.json({ success: true, message: "Live class registration saved!" });
});
// Offline Class Registration
app.post("/api/offline", (req, res) => {
saveToExcel("Offline Classes", req.body);
res.json({ success: true, message: "Offline class registration saved!" });
});
// ------------------------------------------------
app.listen(5000, () => {
console.log("Server running on http://localhost:5000");
});