forked from getyoti/yoti-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·92 lines (78 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require('dotenv').config();
const express = require('express');
const https = require('https');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const Yoti = require('yoti');
const app = express();
const port = process.env.PORT || 9443;
// The application ID and .pem file are generated by https://www.yoti.com/dashboard when you create your app
// The client SDK ID is generated by https://www.yoti.com/dashboard when you publish your app
const config = {
APPLICATION_ID: process.env.YOTI_APPLICATION_ID, // Your Yoti Application ID
CLIENT_SDK_ID: process.env.YOTI_CLIENT_SDK_ID, // Your Yoti Client SDK ID
PEM_KEY: fs.readFileSync(process.env.YOTI_KEY_FILE_PATH), // The content of your Yoti .pem key
};
function saveImage(selfie) {
return new Promise((res, rej) => {
try {
fs.writeFileSync(
path.join(__dirname, 'static', 'YotiSelfie.jpeg'),
selfie.toBase64(),
'base64',
);
res();
} catch (error) {
rej(error);
}
});
}
const yotiClient = new Yoti.Client(config.CLIENT_SDK_ID, config.PEM_KEY);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/static', express.static('static'));
const router = express.Router();
router.get('/', (req, res) => {
res.render('pages/index', {
yotiApplicationId: config.APPLICATION_ID,
});
});
router.get('/profile', (req, res) => {
const { token } = req.query;
if (!token) {
res.render('pages/error', {
error: 'No token has been provided.',
});
return;
}
const promise = yotiClient.getActivityDetails(token);
promise.then((activityDetails) => {
const userProfile = activityDetails.getUserProfile();
const profile = activityDetails.getProfile();
const { selfie } = userProfile;
if (typeof selfie !== 'undefined') {
saveImage(selfie);
}
res.render('pages/profile', {
userId: activityDetails.getUserId(),
selfieUri: activityDetails.getBase64SelfieUri(),
userProfile,
profile,
});
}).catch((err) => {
console.error(err);
res.render('pages/error', {
error: err,
});
});
});
app.use('/', router);
// START THE SERVER
// =============================================================================
https.createServer({
key: fs.readFileSync(path.join(__dirname, 'keys', 'server-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'keys', 'server-cert.pem')),
}, app).listen(port);
console.log(`Server running on https://localhost:${port}`);