@@ -140,22 +140,9 @@ router.post("/login", async (req, res) => {
140
140
}
141
141
} ) ;
142
142
143
- router . get (
144
- "/logout" ,
145
- requireSignIn ,
146
- async ( req : Request , res ) => {
147
- const authenticatedReq = req as AuthenticatedRequest ;
143
+ router . get ( "/logout" , async ( req , res ) => {
148
144
try {
149
- if ( ! authenticatedReq . user ) {
150
- return res . status ( 401 ) . json ( {
151
- ok : false ,
152
- message : "Unauthorized" ,
153
- code : "unauthorized"
154
- } ) ;
155
- }
156
-
157
145
res . clearCookie ( "token" ) ;
158
-
159
146
return res . status ( 200 ) . json ( {
160
147
ok : true ,
161
148
message : "Logged out successfully" ,
@@ -176,13 +163,12 @@ router.get(
176
163
router . get (
177
164
"/current-user" ,
178
165
requireSignIn ,
179
- async ( req : Request , res ) => {
180
- const authenticatedReq = req as AuthenticatedRequest ;
166
+ async ( req : AuthenticatedRequest , res ) => {
181
167
try {
182
- if ( ! authenticatedReq . user ) {
168
+ if ( ! req . user ) {
183
169
return res . status ( 401 ) . json ( { ok : false , error : "Unauthorized" } ) ;
184
170
}
185
- const user = await User . findByPk ( authenticatedReq . user . id , {
171
+ const user = await User . findByPk ( req . user . id , {
186
172
attributes : { exclude : [ "password" ] } ,
187
173
} ) ;
188
174
if ( ! user ) {
@@ -205,7 +191,7 @@ router.get(
205
191
router . get (
206
192
"/user/:id" ,
207
193
requireSignIn ,
208
- async ( req : Request , res ) => {
194
+ async ( req : AuthenticatedRequest , res ) => {
209
195
try {
210
196
const { id } = req . params ;
211
197
if ( ! id ) {
@@ -234,167 +220,102 @@ router.get(
234
220
router . post (
235
221
"/generate-api-key" ,
236
222
requireSignIn ,
237
- async ( req : Request , res ) => {
238
- const authenticatedReq = req as AuthenticatedRequest ;
223
+ async ( req : AuthenticatedRequest , res ) => {
239
224
try {
240
- if ( ! authenticatedReq . user ) {
241
- return res . status ( 401 ) . json ( {
242
- ok : false ,
243
- message : "Unauthorized" ,
244
- code : "unauthorized"
245
- } ) ;
225
+ if ( ! req . user ) {
226
+ return res . status ( 401 ) . json ( { ok : false , error : "Unauthorized" } ) ;
246
227
}
247
-
248
- const user = await User . findByPk ( authenticatedReq . user . id , {
228
+ const user = await User . findByPk ( req . user . id , {
249
229
attributes : { exclude : [ "password" ] } ,
250
230
} ) ;
251
231
252
232
if ( ! user ) {
253
- return res . status ( 404 ) . json ( {
254
- ok : false ,
255
- message : "User not found" ,
256
- code : "not_found"
257
- } ) ;
233
+ return res . status ( 404 ) . json ( { message : "User not found" } ) ;
258
234
}
259
235
260
236
if ( user . api_key ) {
261
- return res . status ( 400 ) . json ( {
262
- ok : false ,
263
- message : "API key already exists" ,
264
- code : "key_exists"
265
- } ) ;
237
+ return res . status ( 400 ) . json ( { message : "API key already exists" } ) ;
266
238
}
267
-
268
239
const apiKey = genAPIKey ( ) ;
240
+
269
241
await user . update ( { api_key : apiKey } ) ;
270
242
271
- // Capture analytics event
272
243
capture ( "maxun-oss-api-key-created" , {
273
244
user_id : user . id ,
274
245
created_at : new Date ( ) . toISOString ( ) ,
275
246
} ) ;
276
247
277
248
return res . status ( 200 ) . json ( {
278
- ok : true ,
279
249
message : "API key generated successfully" ,
280
- api_key : apiKey
250
+ api_key : apiKey ,
281
251
} ) ;
282
-
283
252
} catch ( error ) {
284
- console . error ( 'API Key generation error:' , error ) ;
285
- return res . status ( 500 ) . json ( {
286
- ok : false ,
287
- message : "Error generating API key" ,
288
- code : "server" ,
289
- error : process . env . NODE_ENV === 'development' ? error : undefined
290
- } ) ;
253
+ return res
254
+ . status ( 500 )
255
+ . json ( { message : "Error generating API key" , error } ) ;
291
256
}
292
257
}
293
258
) ;
294
259
295
260
router . get (
296
261
"/api-key" ,
297
262
requireSignIn ,
298
- async ( req : Request , res ) => {
299
- const authenticatedReq = req as AuthenticatedRequest ;
263
+ async ( req : AuthenticatedRequest , res ) => {
300
264
try {
301
- if ( ! authenticatedReq . user ) {
302
- return res . status ( 401 ) . json ( {
303
- ok : false ,
304
- message : "Unauthorized" ,
305
- code : "unauthorized"
306
- } ) ;
265
+ if ( ! req . user ) {
266
+ return res . status ( 401 ) . json ( { ok : false , error : "Unauthorized" } ) ;
307
267
}
308
268
309
- const user = await User . findByPk ( authenticatedReq . user . id , {
269
+ const user = await User . findByPk ( req . user . id , {
310
270
raw : true ,
311
271
attributes : [ "api_key" ] ,
312
272
} ) ;
313
273
314
274
if ( ! user ) {
315
- return res . status ( 404 ) . json ( {
316
- ok : false ,
317
- message : "User not found" ,
318
- code : "not_found"
319
- } ) ;
275
+ return res . status ( 404 ) . json ( { message : "User not found" } ) ;
320
276
}
321
277
322
278
return res . status ( 200 ) . json ( {
323
- ok : true ,
324
279
message : "API key fetched successfully" ,
325
- api_key : user . api_key || null
280
+ api_key : user . api_key || null ,
326
281
} ) ;
327
-
328
282
} catch ( error ) {
329
- console . error ( 'API Key fetch error:' , error ) ;
330
- return res . status ( 500 ) . json ( {
331
- ok : false ,
332
- message : "Error fetching API key" ,
333
- code : "server" ,
334
- error : process . env . NODE_ENV === 'development' ? error : undefined
335
- } ) ;
283
+ return res . status ( 500 ) . json ( { message : "Error fetching API key" , error } ) ;
336
284
}
337
285
}
338
286
) ;
339
287
340
288
router . delete (
341
289
"/delete-api-key" ,
342
290
requireSignIn ,
343
- async ( req : Request , res ) => {
344
- const authenticatedReq = req as AuthenticatedRequest ;
345
- try {
346
- if ( ! authenticatedReq . user ) {
347
- return res . status ( 401 ) . json ( {
348
- ok : false ,
349
- message : "Unauthorized" ,
350
- code : "unauthorized"
351
- } ) ;
352
- }
291
+ async ( req : AuthenticatedRequest , res ) => {
292
+ if ( ! req . user ) {
293
+ return res . status ( 401 ) . send ( { error : "Unauthorized" } ) ;
294
+ }
353
295
354
- const user = await User . findByPk ( authenticatedReq . user . id , {
355
- raw : true ,
356
- attributes : [ "id" , "api_key" ]
357
- } ) ;
296
+ try {
297
+ const user = await User . findByPk ( req . user . id , { raw : true } ) ;
358
298
359
299
if ( ! user ) {
360
- return res . status ( 404 ) . json ( {
361
- ok : false ,
362
- message : "User not found" ,
363
- code : "not_found"
364
- } ) ;
300
+ return res . status ( 404 ) . json ( { message : "User not found" } ) ;
365
301
}
366
302
367
303
if ( ! user . api_key ) {
368
- return res . status ( 404 ) . json ( {
369
- ok : false ,
370
- message : "API Key not found" ,
371
- code : "key_not_found"
372
- } ) ;
304
+ return res . status ( 404 ) . json ( { message : "API Key not found" } ) ;
373
305
}
374
306
375
- await User . update (
376
- { api_key : null } ,
377
- { where : { id : authenticatedReq . user . id } }
378
- ) ;
307
+ await User . update ( { api_key : null } , { where : { id : req . user . id } } ) ;
379
308
380
309
capture ( "maxun-oss-api-key-deleted" , {
381
310
user_id : user . id ,
382
311
deleted_at : new Date ( ) . toISOString ( ) ,
383
312
} ) ;
384
313
385
- return res . status ( 200 ) . json ( {
386
- ok : true ,
387
- message : "API Key deleted successfully"
388
- } ) ;
389
-
390
- } catch ( error ) {
391
- console . error ( 'API Key deletion error:' , error ) ;
392
- return res . status ( 500 ) . json ( {
393
- ok : false ,
394
- message : "Error deleting API key" ,
395
- code : "server" ,
396
- error : process . env . NODE_ENV === 'development' ? error : undefined
397
- } ) ;
314
+ return res . status ( 200 ) . json ( { message : "API Key deleted successfully" } ) ;
315
+ } catch ( error : any ) {
316
+ return res
317
+ . status ( 500 )
318
+ . json ( { message : "Error deleting API key" , error : error . message } ) ;
398
319
}
399
320
}
400
321
) ;
@@ -429,8 +350,7 @@ router.get("/google", (req, res) => {
429
350
router . get (
430
351
"/google/callback" ,
431
352
requireSignIn ,
432
- async ( req : Request , res ) => {
433
- const authenticatedReq = req as AuthenticatedRequest ;
353
+ async ( req : AuthenticatedRequest , res ) => {
434
354
const { code, state } = req . query ;
435
355
try {
436
356
if ( ! state ) {
@@ -456,12 +376,12 @@ router.get(
456
376
return res . status ( 400 ) . json ( { message : "Email not found" } ) ;
457
377
}
458
378
459
- if ( ! authenticatedReq . user ) {
379
+ if ( ! req . user ) {
460
380
return res . status ( 401 ) . send ( { error : "Unauthorized" } ) ;
461
381
}
462
382
463
383
// Get the currently authenticated user (from `requireSignIn`)
464
- let user = await User . findOne ( { where : { id : authenticatedReq . user . id } } ) ;
384
+ let user = await User . findOne ( { where : { id : req . user . id } } ) ;
465
385
466
386
if ( ! user ) {
467
387
return res . status ( 400 ) . json ( { message : "User not found" } ) ;
@@ -539,13 +459,12 @@ router.get(
539
459
router . post (
540
460
"/gsheets/data" ,
541
461
requireSignIn ,
542
- async ( req : Request , res ) => {
543
- const authenticatedReq = req as AuthenticatedRequest ;
462
+ async ( req : AuthenticatedRequest , res ) => {
544
463
const { spreadsheetId, robotId } = req . body ;
545
- if ( ! authenticatedReq . user ) {
464
+ if ( ! req . user ) {
546
465
return res . status ( 401 ) . send ( { error : "Unauthorized" } ) ;
547
466
}
548
- const user = await User . findByPk ( authenticatedReq . user . id , { raw : true } ) ;
467
+ const user = await User . findByPk ( req . user . id , { raw : true } ) ;
549
468
550
469
if ( ! user ) {
551
470
return res . status ( 400 ) . json ( { message : "User not found" } ) ;
@@ -657,14 +576,13 @@ router.post("/gsheets/update", requireSignIn, async (req, res) => {
657
576
router . post (
658
577
"/gsheets/remove" ,
659
578
requireSignIn ,
660
- async ( req : Request , res ) => {
661
- const authenticatedReq = req as AuthenticatedRequest ;
579
+ async ( req : AuthenticatedRequest , res ) => {
662
580
const { robotId } = req . body ;
663
581
if ( ! robotId ) {
664
582
return res . status ( 400 ) . json ( { message : "Robot ID is required" } ) ;
665
583
}
666
584
667
- if ( ! authenticatedReq . user ) {
585
+ if ( ! req . user ) {
668
586
return res . status ( 401 ) . send ( { error : "Unauthorized" } ) ;
669
587
}
670
588
@@ -686,7 +604,7 @@ router.post(
686
604
} ) ;
687
605
688
606
capture ( "maxun-oss-google-sheet-integration-removed" , {
689
- user_id : authenticatedReq . user . id ,
607
+ user_id : req . user . id ,
690
608
robot_id : robotId ,
691
609
deleted_at : new Date ( ) . toISOString ( ) ,
692
610
} ) ;
@@ -700,4 +618,4 @@ router.post(
700
618
} ) ;
701
619
}
702
620
}
703
- ) ;
621
+ ) ;
0 commit comments