Skip to content

Commit 32e153d

Browse files
committed
Logger to store token reset history [subject to change]
1 parent e614634 commit 32e153d

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/controllers/v4/internal/user.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ const processUserAction = async (req, res, next) => {
4646
}
4747

4848
const userId = req.params.id;
49-
const { action, amount, reason, executor, expiry } = req.body; // Extract fields from the request body
49+
const { action, amount, reason, executor, expiry, old_token, new_token, isForced } = req.body; // Extract fields from the request body
50+
51+
if (!action) {
52+
return res.status(400).json({ message: 'Action is required in body' }); // Action is required
53+
}
5054

5155
try {
5256
// Fetch user by ID
@@ -154,6 +158,24 @@ const processUserAction = async (req, res, next) => {
154158
updatedUser = await user.save();
155159
break;
156160

161+
case 'addtokenhistory':
162+
if (!old_token && !new_token) {
163+
return res.status(400).json({ message: 'Old and new tokens are required' });
164+
}
165+
// Update status history
166+
user.token_history.push({
167+
_id: user.token_history.length + 1,
168+
timestamp: new Date(),
169+
reason: reason || 'Self Regenerated',
170+
executor: executor || 'Self',
171+
isForced: isForced || false,
172+
old_token,
173+
new_token,
174+
});
175+
176+
updatedUser = await user.save();
177+
break;
178+
157179
default:
158180
return res.status(400).json({ message: `Invalid action: ${action}` });
159181
}

src/models/schemas/User.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,77 @@ const UserSchema = new mongoose.Schema({
4343
*/
4444
token: { type: String },
4545

46+
/**
47+
* Array to store the history of token resets, including timestamps, reason,
48+
* executor, and old/new token values.
49+
* @type {Array<{
50+
* timestamp: Date,
51+
* reason: string,
52+
* isForced: boolean,
53+
* executor: string,
54+
* old_token: string,
55+
* new_token: string,
56+
* expiry?: Date
57+
* }>}
58+
*/
59+
token_history: [
60+
{
61+
/**
62+
* Unique identifier for the token reset entry.
63+
* @type {string}
64+
* @required
65+
*/
66+
_id: { type: String, required: true },
67+
68+
/**
69+
* Timestamp of the token reset.
70+
* @type {Date}
71+
* @default Date.now
72+
*/
73+
timestamp: { type: Date, default: Date.now },
74+
75+
/**
76+
* Optional expiry time if token is meant to be invalidated after a period.
77+
* @type {Date}
78+
*/
79+
expiry: { type: Date },
80+
81+
/**
82+
* Reason for the token reset (e.g., "suspicious activity", "user request").
83+
* @type {string}
84+
*/
85+
reason: { type: String, default: 'Self Regenerated' },
86+
87+
/**
88+
* Indicates whether the token reset was forced (e.g., by an admin).
89+
* @type {boolean}
90+
* @default false
91+
*/
92+
isForced: { type: Boolean, default: false },
93+
94+
/**
95+
* Information about the staff member or system who performed the reset.
96+
* @type {string}
97+
* @required
98+
*/
99+
executor: { type: String, required: true, default: 'Self' },
100+
101+
/**
102+
* The token before the reset.
103+
* @type {string}
104+
* @required
105+
*/
106+
old_token: { type: String, required: true },
107+
108+
/**
109+
* The token after the reset.
110+
* @type {string}
111+
* @required
112+
*/
113+
new_token: { type: String, required: true },
114+
},
115+
],
116+
46117
/**
47118
* Flag indicating whether the user is banned.
48119
* @type {boolean}

0 commit comments

Comments
 (0)