@@ -29,20 +29,21 @@ const adminHeader = {
2929// Call Incode's `omni/start` API to create an Incode session which will include a
3030// token in the JSON response.
3131app . get ( '/start' , async ( req , res ) => {
32- let uuidParam = req . query . uuid ;
32+ let uniqueId = req . query . uniqueId ;
3333 // We retrieve a session that was already started and from which we have stored.
34- if ( uuidParam ) {
34+ if ( uniqueId ) {
3535 try {
36- const session = readSession ( uuidParam ) ;
37- res . json ( session ) ;
36+ const { token } = readSession ( uniqueId ) ;
37+ // The interviewId is also saved in the session, but you shouldn't return it to the frontend.
38+ res . json ( { token, uniqueId} ) ;
3839 } catch ( e ) {
3940 res . status ( 400 ) . send ( { success :false , error : e . message } ) ;
4041 }
4142 return ;
4243 }
4344
44- // We create a new session
45- uuid = uuidv4 ( ) ;
45+ // We create a new random uniqueId to associate the session
46+ uniqueId = uuidv4 ( ) ;
4647
4748 const startUrl = `${ process . env . API_URL } /omni/start` ;
4849 const startParams = {
@@ -55,8 +56,10 @@ app.get('/start', async (req, res) => {
5556 try {
5657 const startData = await doPost ( startUrl , startParams , defaultHeader ) ;
5758 const { token, interviewId} = startData ;
58- writeSession ( uuid , { token, interviewId, uuid} ) ;
59- res . json ( { token, interviewId, uuid} ) ;
59+ // To the session we save the interviewId for internal purposes
60+ writeSession ( uniqueId , { token, interviewId, uniqueId} ) ;
61+ // But we never return it to the frontend
62+ res . json ( { token, uniqueId} ) ;
6063 } catch ( e ) {
6164 console . log ( e . message ) ;
6265 res . status ( 500 ) . send ( { success :false , error : e . message } ) ;
@@ -323,7 +326,7 @@ app.post('/sign-contract', async (req, res) => {
323326 // if you save tokens at session `/start`, pick the strateggy
324327 // that works best for you.
325328 const sessionData = JSON . parse ( req . body . toString ( ) ) ;
326- const { interviewId, uuid , token} = sessionData ;
329+ const { interviewId, token} = sessionData ;
327330
328331 let contractHeader = { ...defaultHeader } ;
329332 contractHeader [ 'X-Incode-Hardware-Id' ] = token ;
@@ -385,7 +388,7 @@ app.post('/sign-contract', async (req, res) => {
385388 timestamp : new Date ( ) . toISOString ( ) . slice ( 0 , 19 ) . replace ( 'T' , ' ' ) ,
386389 data : contractData
387390 }
388- res . status ( 200 ) . send ( { interviewId, uuid , token, contractData, signatureData} ) ;
391+ res . status ( 200 ) . send ( { interviewId, token, contractData, signatureData} ) ;
389392
390393 // Write to a log so you can debug it.
391394 console . log ( log ) ;
@@ -426,26 +429,32 @@ const doGet = async (url, params, headers) => {
426429 }
427430}
428431
429- function writeSession ( uuid , data ) {
432+ /* Session Helper Functions
433+ * For this example we simply save the session as json file in the /sessions folder
434+ * in a real application you would save this information in a database.
435+ **/
436+
437+ function writeSession ( uniqueId , data ) {
430438 const content = JSON . stringify ( data , null , ' ' ) ;
431439 try {
432- fs . writeFileSync ( `sessions/${ uuid } .json` , content ) ;
440+ fs . writeFileSync ( `sessions/${ uniqueId } .json` , content ) ;
433441 } catch ( err ) {
434442 console . error ( err ) ;
435443 }
436444}
437445
438- function readSession ( uuid ) {
439- if ( ! isUUID ( uuid ) || ! fs . existsSync ( `sessions/${ uuid } .json` ) ) {
440- throw new Error ( 'Invalid uuid ' ) ;
446+ function readSession ( uniqueId ) {
447+ if ( ! isUUID ( uniqueId ) || ! fs . existsSync ( `sessions/${ uniqueId } .json` ) ) {
448+ throw new Error ( 'Invalid uniqueId ' ) ;
441449 }
442- const rawData = fs . readFileSync ( `sessions/${ uuid } .json` , { encoding : 'utf8' , flag : 'r' } ) ;
450+ const rawData = fs . readFileSync ( `sessions/${ uniqueId } .json` , { encoding : 'utf8' , flag : 'r' } ) ;
443451 try {
444452 return JSON . parse ( rawData ) ;
445453 } catch ( e ) {
446454 throw new Error ( 'Session data corrupted' ) ;
447455 }
448456}
457+ /* End Session Helper Functions **/
449458
450459// Listen for HTTP
451460const httpPort = 3000 ;
0 commit comments