Skip to content

Commit 140bb1a

Browse files
committed
clear conflict
1 parent 1bb1873 commit 140bb1a

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

backend/src/controllers/authController.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ export const registerUser = async (req: Request, res: Response, next: NextFuncti
3636
const typedUser = asTypedUser(newUser);
3737

3838
const token = generateToken(typedUser._id.toString());
39-
const decoded = jwt.decode(token) as { exp?: number };
40-
const expiresAt = new Date((decoded.exp ?? 0) * 1000);
39+
const decoded = jwt.decode(token) as { exp?: number } | null;
4140

41+
if (!decoded || !decoded.exp) {
42+
throw new Error("Invalid token format or missing expiration");
43+
}
44+
45+
const expiresAt = new Date(decoded.exp * 1000);
4246
await Session.create({
4347
userId: typedUser._id,
4448
token,
@@ -85,8 +89,13 @@ export const loginUser = async (req: Request, res: Response, next: NextFunction)
8589
const typedUser = asTypedUser(foundUser);
8690

8791
const token = generateToken(typedUser._id.toString());
88-
const decoded = jwt.decode(token) as { exp?: number };
89-
const expiresAt = new Date((decoded.exp ?? 0) * 1000);
92+
const decoded = jwt.verify(token, process.env.JWT_SECRET!) as { exp?: number };
93+
94+
if (!decoded.exp) {
95+
throw new Error("Token missing expiration claim");
96+
}
97+
98+
const expiresAt = new Date(decoded.exp * 1000);
9099

91100
await Session.create({
92101
userId: typedUser._id,

backend/src/server.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createServer } from "http";
44
import { Server as SocketIOServer } from "socket.io";
55
import dotenv from "dotenv";
66
import cors from "cors";
7-
import cron from "node-cron";
7+
88
import { Session } from "./models/sessionModel.js";
99
import { ChatMessage } from "./models/chatMessageModel.js"; // <-- make sure this file exists and exports model
1010
import app from "./app.js";
@@ -105,16 +105,16 @@ mongoose
105105
console.log("🗄️ MongoDB connected successfully!");
106106

107107

108-
cron.schedule("0 2 * * *", async () => {
109-
const expiryDate = new Date();
110-
expiryDate.setDate(expiryDate.getDate() - 7);
111-
try {
112-
const result = await Session.deleteMany({ createdAt: { $lt: expiryDate } });
113-
console.log(`🧹 Cleanup complete — ${result.deletedCount} expired sessions removed`);
114-
} catch (error) {
115-
console.error("❌ Session cleanup failed:", error);
116-
}
117-
});
108+
// cron.schedule("0 2 * * *", async () => {
109+
// const expiryDate = new Date();
110+
// expiryDate.setDate(expiryDate.getDate() - 7);
111+
// try {
112+
// const result = await Session.deleteMany({ createdAt: { $lt: expiryDate } });
113+
// console.log(`🧹 Cleanup complete — ${result.deletedCount} expired sessions removed`);
114+
// } catch (error) {
115+
// console.error("❌ Session cleanup failed:", error);
116+
// }
117+
// });
118118

119119

120120
httpServer.listen(PORT, () => {

0 commit comments

Comments
 (0)