-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
122 lines (103 loc) · 2.83 KB
/
server.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const Hapi = require('hapi');
const Inert = require('inert');
const fs = require('fs');
const path = require('path');
const Yoti = require('yoti-node-sdk');
const Vision = require('vision');
const CLIENT_SDK_ID = 'e2d30c07-90e8-4e68-bdd1-691b4242d0f9';
const server = new Hapi.Server();
const yoti_key = fs.readFileSync(path.join(__dirname, './keys/yoti-key.pem'));
const client = new Yoti(CLIENT_SDK_ID, yoti_key);
let tls = {};
if(process.env.ENVIRONMENT !== 'deployment') {
tls = {
key: fs.readFileSync(path.join(__dirname, './keys/key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(__dirname, './keys/cert.pem'), 'utf8'),
};
}
let users = {};
function userSignIn (user) {
users[user.userId] = {
userId: user.userId,
email: user.profile.emailAddress,
login_time: Date.now(),
}
}
function generateTimeString(milliseconds) {
const totalMinutes = milliseconds / 60000;
const hours = Math.floor(totalMinutes / 60);
const minutes = Math.floor(totalMinutes % 60);
const hoursString = (hours ? `${hours} hour${hours > 1 ? 's ' : ' '}`: '');
return `${hoursString}${minutes} minutes`;
}
const routes = [
{
method: 'GET',
path: '/',
handler: (request, reply) => {
return reply.view('index.hbs');
}
},
{
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'public'
}
}
},
{
method: 'GET',
path: '/event/1/start',
handler: (request, reply) => {
const token = request.query.token;
if(!token) { return reply('No token found'); }
client.getActivityDetails(token)
.then(activityDetails => {
let user = {
userId: activityDetails.getUserId(),
profile: activityDetails.getUserProfile(),
};
if (users[user.userId]) {
totalTime = Date.now() - users[user.userId].login_time;
delete users[user.userId];
return reply.view('thank-you.hbs', {
name: user.profile.givenNames,
time: generateTimeString(totalTime)
})
}
userSignIn(user);
return reply.view('welcome.hbs', {
name: user.profile.givenNames
});
})
.catch(err => {
console.log(err);
return reply('Error');
});
}
}
];
server.register([Inert, Vision], (err) => {
let options = {
port: process.env.PORT || 3456
};
if(process.env.ENVIRONMENT !== "deployment") {
options.tls = tls;
}
server.connection(options);
server.views({
engines: {
hbs: require('handlebars'),
html: require('handlebars')
},
relativeTo: __dirname,
path: './public/templates',
layoutPath: './public/templates/layout',
layout: 'default'
});
server.route(routes);
server.start(() => { console.log((`Server running at: ${server.info.uri}`)); })
});
module.exports = server;