Skip to content

Commit 7dbc41c

Browse files
Merge pull request #6 from Incode-Technologies-Example-Repos/uuid-to-uniqueId
changed uuid to uniqueId to avoid confusion with internal uuid
2 parents 38777b4 + c99ed91 commit 7dbc41c

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

sample-server/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Endpoints
44

5-
- GET `/start`: Call Incode's `/omni/start` API to create an Incode session which will include a token in the JSON response. This token can be shared with Incode SDK client apps to do token based initialization, which is a best practice.
5+
- GET `/start`: Call Incode's `/omni/start` API to create an Incode session which will include a `token` in the JSON response. This token can be shared with Incode SDK client apps to do token based initialization, which is a best practice.
66

77
It also performs basic storage of sessions in the `sessions` directory to help implement `renderRedirectToMobile`in frontend.
88

9-
At session generation it will generate an `uuid` and save the session in `session/<UUID>.json`, later if you call `/start` again passing a valid `uuid` it will retrieve the stored session instead of creating a new one.
9+
At session generation it will generate an `uniqueId` and save the session in `session/<uniqueId>.json`, later if you call `/start` again passing a valid `uniqueId` it will retrieve the stored session instead of creating a new one.
1010

1111
- GET `/onboarding-url`: Calls incodes `/omni/start` and then with the token calls `/0/omni/onboarding-url` to retrieve the unique onboarding-url for the newly created session.
1212

@@ -94,7 +94,7 @@ Open the `Forwarding` adress in a web browser. The URL should look similar to th
9494

9595
Now you should be able to visit the following routes to receive the associated payloads:
9696
1. `https://yourforwardingurl.app/start`
97-
2. `https://yourforwardingurl.app/start?uuid=0e810732-6e7e-4512-aaa5-1ae2e1f8df46`
97+
2. `https://yourforwardingurl.app/start?uniqueId=0e810732-6e7e-4512-aaa5-1ae2e1f8df46`
9898
3. `https://yourforwardingurl.app/onboarding-url`
9999
4. `https://yourforwardingurl.app/onboarding-url?redirectionUrl=https%3A%2F%2Fexample.com%2F`
100100

sample-server/index.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3131
app.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
451460
const httpPort = 3000;

0 commit comments

Comments
 (0)