11const express = require ( 'express' ) ;
2- const fetch = require ( 'node-fetch' ) ;
2+ const fetch = ( ...args ) =>
3+ import ( 'node-fetch' ) . then ( ( { default : fetch } ) => fetch ( ...args ) ) ;
34const app = express ( ) ;
45const https = require ( 'https' ) ;
56const cors = require ( 'cors' ) ;
@@ -14,42 +15,92 @@ app.use(
1415 cors ( {
1516 origin : '*' ,
1617 methods : [ 'GET' , 'POST' ] ,
18+ allowedHeaders : [ 'Content-Type' , 'Authorization' , 'Accept' ] ,
19+ credentials : true ,
20+ exposedHeaders : [ 'Content-Type' , 'Authorization' ] ,
1721 } )
1822) ;
1923
2024app . use ( express . static ( 'public' ) ) ;
2125
2226app . post ( '/api/auth' , jsonParser , async ( req , res ) => {
23- const loginUrl = 'https://ci-1.corellium.co/api/v1/webplayer' ; // this domain would be https://app.corellium.co/api/v1/webplayer in production
27+ console . log ( 'Received auth request at:' , new Date ( ) . toISOString ( ) ) ;
28+ console . log ( 'Request headers:' , req . headers ) ;
2429
25- const httpsAgent = new https . Agent ( {
26- rejectUnauthorized : false ,
30+ // Change this URL to your domain URL
31+ const baseUrl = '' ; // example - https://app.corellium.co in production
32+
33+ const loginUrl = `${ baseUrl } /api/v1/webplayer` ;
34+
35+ console . log ( 'Incoming request data:' , {
36+ projectId : req . body ?. projectId ,
37+ instanceId : req . body ?. instanceId ,
38+ features : req . body ?. features ,
39+ hasToken : ! ! req . body ?. token ,
2740 } ) ;
2841
42+ const options = {
43+ method : 'POST' ,
44+ headers : {
45+ 'Content-Type' : 'application/json' ,
46+ Authorization : req . body . token ,
47+ Accept : 'application/json' ,
48+ } ,
49+ body : JSON . stringify ( {
50+ projectId : req . body . projectId ,
51+ instanceId : req . body . instanceId ,
52+ expiresIn : 60 * 60 * 5 ,
53+ features : req . body . features ,
54+ } ) ,
55+ agent : new https . Agent ( {
56+ rejectUnauthorized : false ,
57+ } ) ,
58+ } ;
59+
2960 try {
30- const response = await fetch ( loginUrl , {
31- method : 'POST' ,
32- headers : {
33- 'Content-Type' : 'application/json' ,
34- Authorization : req . body . token , // your API token
35- } ,
36- body : JSON . stringify ( {
37- projectId : req . body . projectId ,
38- instanceId : req . body . instanceId ,
39- expiresIn : 60 * 60 * 5 , // value is in seconds
40- features : req . body . features ,
41- } ) ,
42- agent : httpsAgent ,
61+ console . log ( 'Making request to:' , loginUrl ) ;
62+ console . log ( 'Request options:' , {
63+ method : options . method ,
64+ headers : options . headers ,
65+ body : JSON . parse ( options . body ) ,
4366 } ) ;
4467
68+ const response = await fetch ( loginUrl , options ) ;
69+
70+ console . log ( 'Response status:' , response . status ) ;
71+ console . log ( 'Response headers:' , response . headers . raw ( ) ) ;
72+
73+ if ( ! response . ok ) {
74+ const errorText = await response . text ( ) ;
75+ console . error ( 'Error response body:' , errorText ) ;
76+ throw new Error (
77+ `HTTP error! status: ${ response . status } , body: ${ errorText } `
78+ ) ;
79+ }
80+
4581 const data = await response . json ( ) ;
82+ console . log ( 'Success response:' , data ) ;
4683
47- res . send ( data ) ;
84+ // Set explicit headers for the response
85+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
86+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
87+ res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST' ) ;
88+ res . setHeader (
89+ 'Access-Control-Allow-Headers' ,
90+ 'Content-Type, Authorization, Accept'
91+ ) ;
4892
49- return ;
93+ console . log ( 'Sending response to client' ) ;
94+ res . send ( data ) ;
95+ console . log ( 'Response sent successfully' ) ;
5096 } catch ( err ) {
51- console . log ( 'webplayer ERROR: ' , err ) ;
52- throw new Error ( err ) ;
97+ console . error ( 'Webplayer Error:' , err . message ) ;
98+ console . error ( 'Error stack:' , err . stack ) ;
99+ res . status ( 500 ) . json ( {
100+ error : 'Failed to connect to webplayer service' ,
101+ details : err . message ,
102+ status : err . status || 500 ,
103+ } ) ;
53104 }
54105} ) ;
55106
0 commit comments