@@ -61,81 +61,121 @@ const updateUserToken = async (req, res, next) => {
6161} ;
6262
6363/**
64- * Handles user-related operations based on the HTTP method.
64+ * Processes user session by creating a new user if one doesn't exist,
65+ * updating tokens if applicable, and handling authentication.
6566 *
66- * @param {Object } req - Express request object.
67- * @param {Object } res - Express response object.
68- * @param {Function } next - Express next middleware function.
67+ * @param {Object } req - Express request object containing headers and body.
68+ * @param {Object } res - Express response object for sending responses.
69+ * @param {Function } next - Express next middleware function for error handling.
70+ * @returns {Object } - JSON response indicating success or failure.
6971 */
70- const userEndpoint = async ( req , res , next ) => {
72+ const processUserSessionAndUpdate = async ( req , res , next ) => {
7173 try {
72- const { body, headers, method } = req ;
74+ const { headers, body } = req ;
75+ const { token, id, email, 'access-token' : access_token } = body ;
7376 const { key } = headers ;
7477
75- // Check for valid access key in headers
78+ // Validate access key
7679 if ( ! key || key !== process . env . ACCESS_KEY ) {
77- return res . status ( 401 ) . json ( {
78- message : 'Unauthorized' ,
79- } ) ;
80+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
8081 }
8182
82- if ( method === 'POST' ) {
83- const { token, id } = body ;
83+ // Validate User ID
84+ if ( ! id ) {
85+ return res . status ( 400 ) . json ( { message : 'User ID is required' } ) ;
86+ }
8487
85- // Check for required fields in the request body
86- if ( ! token || ! id ) {
88+ // Check if the user exists
89+ const existingUser = await Users . findOne ( { _id : id } ) ;
90+
91+ if ( ! existingUser ) {
92+ // If the user doesn't exist, ensure required fields are provided
93+ if ( ! email || ! access_token ) {
8794 return res . status ( 400 ) . json ( {
88- message : 'Token and User ID are required in the request body ' ,
95+ message : 'Email and access-token are required for new users ' ,
8996 } ) ;
9097 }
9198
92- // Update user's token in the database
93- await Users . updateOne (
94- { _id : { $eq : id } } ,
95- { $set : { token : token } } ,
96- { upsert : true } , // Create the document if it doesn't exist
97- ) ;
98-
99- return res . status ( 200 ) . json ( {
100- message : 'Token updated successfully' ,
99+ // Create a new user with a generated token
100+ const generatedToken = generateToken ( id , process . env . HMAC_KEY ) ;
101+ const newUser = {
102+ _id : id ,
103+ email,
104+ token : generatedToken ,
105+ access_token,
106+ password : crypto . randomBytes ( 22 ) . toString ( 'base64' ) , // Generate a random password
107+ } ;
108+
109+ await Users . create ( newUser ) ;
110+
111+ return res . status ( 201 ) . json ( {
112+ message : 'User created successfully' ,
113+ token : newUser . token ,
101114 } ) ;
102- } else if ( method === 'GET' ) {
103- const { id, email } = headers ;
115+ } else {
116+ // If the user exists, update the token if provided, and access-token if available
117+ const updates = { } ;
118+ if ( token ) updates . token = token ;
119+ if ( access_token ) updates . access_token = access_token ;
104120
105- // Check for required User ID in the headers
106- if ( ! id ) {
107- return res . status ( 400 ) . json ( {
108- message : 'User ID missing in the request body' ,
109- } ) ;
121+ if ( Object . keys ( updates ) . length > 0 ) {
122+ await Users . updateOne ( { _id : id } , { $set : updates } ) ;
110123 }
111124
112- // Fetch user details based on the provided user ID
113- const user = await Users . findOne ( { _id : { $eq : id } } ) ;
125+ if ( token ) {
126+ return res . status ( 200 ) . json ( { message : 'Token updated successfully' , token : token } ) ;
127+ } else {
128+ return res . status ( 200 ) . json ( { message : 'Logging successfully' , token : existingUser . token } ) ;
129+ }
130+ }
131+ } catch ( error ) {
132+ console . error ( 'Error in processUserSessionAndUpdate :' , error . message ) ;
133+ return next ( error ) ;
134+ }
135+ } ;
114136
115- if ( ! user ) {
116- // If user not found, create a new user with the provided ID and token
117- const newUser = {
118- _id : id ,
119- email : email ,
120- password : crypto . randomBytes ( 22 ) . toString ( 'base64' ) ,
121- token : generateToken ( id , process . env . HMAC_KEY ) ,
122- // Add other fields in the "newUser" object based on your schema
123- } ;
137+ /**
138+ * Fetches user data by ID, validates the access key, and updates the access token if provided.
139+ *
140+ * @param {Object } req - Express request object containing headers.
141+ * @param {Object } res - Express response object for sending responses.
142+ * @param {Function } next - Express next middleware function for error handling.
143+ * @returns {Object } - JSON response with user token or error message.
144+ */
145+ const getUser = async ( req , res , next ) => {
146+ try {
147+ const { headers } = req ;
148+ const { key } = headers ;
124149
125- await Users . create ( newUser ) ;
150+ // Validate access key
151+ if ( ! key || key !== process . env . ACCESS_KEY ) {
152+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
153+ }
126154
127- return res . status ( 201 ) . json ( newUser . token ) ;
128- }
155+ const { id, email, 'access-token' : access_token } = headers ;
129156
130- return res . status ( 200 ) . json ( user . token ) ;
131- } else {
132- return res . status ( 405 ) . json ( {
133- message : 'Method Not Allowed' ,
134- } ) ;
157+ // Validate User ID
158+ if ( ! id ) {
159+ return res . status ( 400 ) . json ( { message : 'User ID is required' } ) ;
160+ }
161+
162+ // Find user by ID
163+ const user = await Users . findOne ( { _id : id } ) ;
164+
165+ if ( ! user ) {
166+ return res . status ( 404 ) . json ( { message : 'User not found' } ) ;
135167 }
168+
169+ // Update user's access token
170+ if ( access_token ) {
171+ await Users . updateOne ( { _id : id } , { $set : { access_token } } ) ;
172+ }
173+
174+ return res . status ( 200 ) . json ( { token : user . token } ) ;
136175 } catch ( error ) {
137- return next ( error ) ;
176+ console . error ( 'Error in getUser:' , error . message ) ;
177+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } ) ;
138178 }
139179} ;
140180
141- export { userEndpoint , retrieveUserProfile , updateUserToken } ;
181+ export { retrieveUserProfile , updateUserToken , processUserSessionAndUpdate , getUser } ;
0 commit comments