1818 */
1919
2020const BaseService = require ( '../../../services/BaseService' ) ;
21- const { Endpoint } = require ( '../../../util/expressutil ' ) ;
21+ const eggspress = require ( '../../../api/eggspress ' ) ;
2222const { checkCaptcha } = require ( '../middleware/captcha-middleware' ) ;
2323
2424/**
@@ -129,36 +129,32 @@ class CaptchaService extends BaseService {
129129 app . use ( '/api/captcha' , api ) ;
130130
131131 // Generate captcha endpoint
132- Endpoint ( {
133- route : '/generate' ,
134- methods : [ 'GET' ] ,
135- handler : async ( req , res ) => {
136- const captcha = this . generateCaptcha ( ) ;
137- res . json ( {
138- token : captcha . token ,
139- image : captcha . data ,
140- } ) ;
141- } ,
142- } ) . attach ( api ) ;
132+ api . use ( eggspress ( '/generate' , {
133+ allowedMethods : [ 'GET' ] ,
134+ } , async ( req , res ) => {
135+ const captcha = this . generateCaptcha ( ) ;
136+ res . json ( {
137+ token : captcha . token ,
138+ image : captcha . data ,
139+ } ) ;
140+ } ) ) ;
143141
144142 // Verify captcha endpoint
145- Endpoint ( {
146- route : '/verify' ,
147- methods : [ 'POST' ] ,
148- handler : ( req , res ) => {
149- const { token, answer } = req . body ;
150-
151- if ( ! token || ! answer ) {
152- return res . status ( 400 ) . json ( {
153- valid : false ,
154- error : 'Missing token or answer' ,
155- } ) ;
156- }
143+ api . use ( eggspress ( '/verify' , {
144+ allowedMethods : [ 'POST' ] ,
145+ } , ( req , res ) => {
146+ const { token, answer } = req . body ;
147+
148+ if ( ! token || ! answer ) {
149+ return res . status ( 400 ) . json ( {
150+ valid : false ,
151+ error : 'Missing token or answer' ,
152+ } ) ;
153+ }
157154
158- const isValid = this . verifyCaptcha ( token , answer ) ;
159- res . json ( { valid : isValid } ) ;
160- } ,
161- } ) . attach ( api ) ;
155+ const isValid = this . verifyCaptcha ( token , answer ) ;
156+ res . json ( { valid : isValid } ) ;
157+ } ) ) ;
162158
163159 // Special endpoint for automated testing
164160 // This should be disabled in production
@@ -430,8 +426,10 @@ class CaptchaService extends BaseService {
430426 // Invalid token or expired
431427 if ( ! captchaData ) {
432428 console . log ( 'Verification FAILED: No data found for this token' ) ;
433- console . log ( 'TOKENS_TRACKING: Available tokens (first 8 chars):' ,
434- Array . from ( this . captchaTokens . keys ( ) ) . map ( t => t . substring ( 0 , 8 ) ) ) ;
429+ console . log (
430+ 'TOKENS_TRACKING: Available tokens (first 8 chars):' ,
431+ Array . from ( this . captchaTokens . keys ( ) ) . map ( t => t . substring ( 0 , 8 ) ) ,
432+ ) ;
435433 this . log . debug ( `Invalid captcha token: ${ token } ` ) ;
436434 return false ;
437435 }
@@ -542,29 +540,29 @@ class CaptchaService extends BaseService {
542540 } ;
543541
544542 switch ( this . difficulty ) {
545- case 'easy' :
546- return {
547- ...baseOptions ,
548- size : 4 ,
549- width : 150 ,
550- height : 50 ,
551- noise : 1 ,
552- } ;
553- case 'hard' :
554- return {
555- ...baseOptions ,
556- size : 7 ,
557- width : 200 ,
558- height : 60 ,
559- noise : 3 ,
560- } ;
561- case 'medium' :
562- default :
563- return {
564- ...baseOptions ,
565- width : 180 ,
566- height : 50 ,
567- } ;
543+ case 'easy' :
544+ return {
545+ ...baseOptions ,
546+ size : 4 ,
547+ width : 150 ,
548+ height : 50 ,
549+ noise : 1 ,
550+ } ;
551+ case 'hard' :
552+ return {
553+ ...baseOptions ,
554+ size : 7 ,
555+ width : 200 ,
556+ height : 60 ,
557+ noise : 3 ,
558+ } ;
559+ case 'medium' :
560+ default :
561+ return {
562+ ...baseOptions ,
563+ width : 180 ,
564+ height : 50 ,
565+ } ;
568566 }
569567 }
570568
@@ -643,4 +641,4 @@ class CaptchaService extends BaseService {
643641
644642// Export both as a named export and as a default export for compatibility
645643module . exports = CaptchaService ;
646- module . exports . CaptchaService = CaptchaService ;
644+ module . exports . CaptchaService = CaptchaService ;
0 commit comments