@@ -3,14 +3,14 @@ import Users from '../../../models/schemas/User.js';
3
3
import generateToken from '../../../modules/generateToken.js' ;
4
4
5
5
/**
6
- * Fetches user profile data based on the provided user ID.
6
+ * Fetches user profile data based on the provided user ID and Reset Token .
7
7
*
8
8
* @param {Object } req - Express request object.
9
9
* @param {Object } res - Express response object.
10
10
* @param {Function } next - Express next middleware function.
11
11
* @returns {Object } - User profile data.
12
12
*/
13
- const getUserProfile = async ( req , res , next ) => {
13
+ const retrieveAndUpdateUserProfile = async ( req , res , next ) => {
14
14
const key = req . headers . key ;
15
15
// Check for valid access key in headers
16
16
if ( ! key || key !== process . env . ACCESS_KEY ) {
@@ -23,6 +23,14 @@ const getUserProfile = async (req, res, next) => {
23
23
return res . status ( 404 ) . json ( { message : 'User not found' } ) ; // User not found
24
24
}
25
25
26
+ // Update user's token in the database
27
+ await Users . updateOne (
28
+ { _id : { $eq : req . params . id } } ,
29
+ { $set : { token : generateToken ( req . params . id , process . env . HMAC_KEY ) } } ,
30
+ { upsert : true } , // Create the document if it doesn't exist
31
+ ) ;
32
+
33
+ // This will return the data however it won't be the latest one after updating the token
26
34
return res . status ( 200 ) . json ( user ) ;
27
35
} ;
28
36
@@ -104,4 +112,4 @@ const userEndpoint = async (req, res, next) => {
104
112
}
105
113
} ;
106
114
107
- export { userEndpoint , getUserProfile } ;
115
+ export { userEndpoint , retrieveAndUpdateUserProfile } ;
0 commit comments