Skip to content

Commit e0136d1

Browse files
committed
feat: implement sliding session window with automatic token refresh on user activity
1 parent 0e3bb1d commit e0136d1

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

backend/src/middleware/auth.middleware.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ const jwt = require("jsonwebtoken");
22
const { User } = require("../models");
33

44
const JWT_SECRET = process.env.JWT_SECRET;
5-
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || "3600";
5+
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || 14400; // default 4 hours
6+
const MAX_SESSION_DURATION = process.env.MAX_SESSION_DURATION || "86400"; // 24 hours default
67

7-
const setTokenCookie = (res, user) => {
8+
const setTokenCookie = (res, user, isNewSession = true) => {
89
// create safe user object for token
910
const safeUser = {
1011
id: user.id,
1112
email: user.email,
1213
username: user.username,
1314
};
1415

15-
// Add session start time for new logins
16+
// Add
1617
const payload = {
1718
data: safeUser,
1819
};
1920

20-
// If this is a new session, add the session start time
21+
// Add: If this is a new session, add the session start time
2122
if (isNewSession) {
2223
payload.sessionStart = Math.floor(Date.now() / 1000); // Unix timestamp
2324
} else {
@@ -26,7 +27,11 @@ const setTokenCookie = (res, user) => {
2627
}
2728

2829
// sign JWT token
29-
const token = jwt.sign({ data: safeUser }, JWT_SECRET, {
30+
// const token = jwt.sign({ data: safeUser }, JWT_SECRET, {
31+
// expiresIn: parseInt(JWT_EXPIRES_IN),
32+
// });
33+
// replace with
34+
const token = jwt.sign(payload, JWT_SECRET, {
3035
expiresIn: parseInt(JWT_EXPIRES_IN),
3136
});
3237

@@ -67,14 +72,11 @@ const restoreUser = (req, res, next) => {
6772
// extract user id from token payload
6873
const { id } = jwtPayload.data;
6974

70-
// Check maximum session duration (e.g., 24 hours)
71-
const MAX_SESSION_DURATION = parseInt(
72-
process.env.MAX_SESSION_DURATION || "86400"
73-
); // 24 hours default
75+
// Add: Check maximum session duration
7476
const currentTime = Math.floor(Date.now() / 1000);
7577
const sessionAge = currentTime - jwtPayload.sessionStart;
7678

77-
if (sessionAge > MAX_SESSION_DURATION) {
79+
if (sessionAge > parseInt(MAX_SESSION_DURATION)) {
7880
// Session has exceeded maximum duration
7981
res.clearCookie("token");
8082
return next();
@@ -88,7 +90,7 @@ const restoreUser = (req, res, next) => {
8890
},
8991
});
9092

91-
// refresh token - issue new token with extended expiration
93+
// Add: refresh token - issue new token with extended expiration
9294
if (req.user) {
9395
req.user.sessionStart = jwtPayload.sessionStart; // Pass along the original session start
9496
setTokenCookie(res, req.user, false);

src/components/User/Dashboard/DatasetOrganizer/LLMPanel.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// src/components/DatasetOrganizer/LLMPanel.tsx
21
import { Close, ContentCopy, Download, AutoAwesome } from "@mui/icons-material";
32
import {
43
Box,
@@ -179,6 +178,8 @@ const LLMPanel: React.FC<LLMPanelProps> = ({ files, onClose }) => {
179178
FILE STRUCTURE AND METADATA:
180179
${fileSummary}
181180
181+
all _sourcePath are relative to the root path /Users/elaine/Downloads
182+
182183
Please generate a Python script that:
183184
1. Reads the source files
184185
2. Renames and reorganizes them according to BIDS specification

0 commit comments

Comments
 (0)