@@ -2,6 +2,31 @@ import crypto from 'crypto';
2
2
import Users from '../../../models/schemas/User.js' ;
3
3
import generateToken from '../../../modules/generateToken.js' ;
4
4
5
+ /**
6
+ * Fetches user profile data based on the provided user ID
7
+ *
8
+ * @param {Object } req - Express request object.
9
+ * @param {Object } res - Express response object.
10
+ * @param {Function } next - Express next middleware function.
11
+ * @returns {Object } - User profile data.
12
+ */
13
+ const retrieveUserProfile = async ( req , res , next ) => {
14
+ const key = req . headers . key ;
15
+ // Check for valid access key in headers
16
+ if ( ! key || key !== process . env . ACCESS_KEY ) {
17
+ return res . status ( 401 ) . json ( {
18
+ message : 'Unauthorized' ,
19
+ } ) ;
20
+ }
21
+ const user = await Users . findById ( req . params . id ) ;
22
+ if ( ! user ) {
23
+ return res . status ( 404 ) . json ( { message : 'User not found' } ) ; // User not found
24
+ }
25
+
26
+ // This will return the data however it won't be the latest one after updating the token
27
+ return res . status ( 200 ) . json ( user ) ;
28
+ } ;
29
+
5
30
/**
6
31
* Fetches user profile data based on the provided user ID and Reset Token.
7
32
*
@@ -10,7 +35,7 @@ import generateToken from '../../../modules/generateToken.js';
10
35
* @param {Function } next - Express next middleware function.
11
36
* @returns {Object } - User profile data.
12
37
*/
13
- const retrieveAndUpdateUserProfile = async ( req , res , next ) => {
38
+ const updateUserToken = async ( req , res , next ) => {
14
39
const key = req . headers . key ;
15
40
// Check for valid access key in headers
16
41
if ( ! key || key !== process . env . ACCESS_KEY ) {
@@ -27,11 +52,12 @@ const retrieveAndUpdateUserProfile = async (req, res, next) => {
27
52
await Users . updateOne (
28
53
{ _id : { $eq : req . params . id } } ,
29
54
{ $set : { token : generateToken ( req . params . id , process . env . HMAC_KEY ) } } ,
30
- { upsert : true } , // Create the document if it doesn't exist
31
55
) ;
32
56
33
57
// This will return the data however it won't be the latest one after updating the token
34
- return res . status ( 200 ) . json ( user ) ;
58
+ return res . status ( 200 ) . json ( {
59
+ message : 'Token reset successfully.' ,
60
+ } ) ;
35
61
} ;
36
62
37
63
/**
@@ -112,4 +138,4 @@ const userEndpoint = async (req, res, next) => {
112
138
}
113
139
} ;
114
140
115
- export { userEndpoint , retrieveAndUpdateUserProfile } ;
141
+ export { userEndpoint , retrieveUserProfile , updateUserToken } ;
0 commit comments