Skip to content

Commit a402303

Browse files
authored
Merge pull request #13 from PerfectFit-project/348-tokens-expiration
348 tokens expiration
2 parents ec2fa0f + e1e4e35 commit a402303

File tree

8 files changed

+254
-28
lines changed

8 files changed

+254
-28
lines changed

niceday-api/controllers/FileSharing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const FileSharing = require('../service/FilesSharingService');
44
module.exports.uploadFile = function uploadFile(req, res, next, body) {
55
FileSharing.uploadFile(req, body)
66
.then((response) => {
7+
console.log('Uploaded file: ', response);
78
utils.writeJson(res, response);
89
})
910
.catch((response) => {
10-
utils.writeJson(res, response);
11+
utils.writeJson(res, utils.respondWithCode(500, response));
1112
});
1213
};

niceday-api/index.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { Authentication, SenseServer } = require('@sense-os/goalie-js');
2+
const schedule = require('node-schedule');
23
const path = require('path');
34
const http = require('http');
45
const oas3Tools = require('oas3-tools');
@@ -9,27 +10,28 @@ require('dotenv').config();
910

1011
const serverPort = 8080;
1112

12-
function createNicedayApiServer() {
13-
const { THERAPIST_PASSWORD, THERAPIST_EMAIL_ADDRESS, ENVIRONMENT } = process.env;
14-
let selectedServer;
15-
16-
if (ENVIRONMENT === 'dev') {
17-
selectedServer = SenseServer.Alpha;
18-
} else {
19-
selectedServer = SenseServer.Production;
20-
}
21-
22-
const authSdk = new Authentication(selectedServer);
23-
// swaggerRouter configuration
24-
const options = {
25-
routing: {
26-
controllers: path.join(__dirname, './controllers'),
27-
},
28-
};
29-
30-
const expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
31-
const app = expressAppConfig.getApp();
13+
const { THERAPIST_PASSWORD, THERAPIST_EMAIL_ADDRESS, ENVIRONMENT } = process.env;
14+
let selectedServer;
15+
16+
if (ENVIRONMENT === 'dev') {
17+
selectedServer = SenseServer.Alpha;
18+
} else {
19+
selectedServer = SenseServer.Production;
20+
}
21+
22+
const authSdk = new Authentication(selectedServer);
3223

24+
// swaggerRouter configuration
25+
const options = {
26+
routing: {
27+
controllers: path.join(__dirname, './controllers'),
28+
},
29+
};
30+
31+
const expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
32+
const app = expressAppConfig.getApp();
33+
34+
function createNicedayApiServer() {
3335
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
3436
.then((response) => {
3537
app.set('therapistId', response.user.id);
@@ -45,10 +47,31 @@ function createNicedayApiServer() {
4547
return server;
4648
}
4749

50+
function setupTokenRegeneration() {
51+
const rule = new schedule.RecurrenceRule();
52+
rule.hour = new schedule.Range(0, 23, 8);
53+
54+
const job = schedule.scheduleJob(rule, () => {
55+
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
56+
.then((response) => {
57+
app.set('therapistId', response.user.id);
58+
app.set('token', response.token);
59+
})
60+
.catch((error) => {
61+
throw Error(`Error during authentication: ${error}`);
62+
});
63+
});
64+
65+
return job;
66+
}
67+
4868
module.exports.createNicedayApiServer = createNicedayApiServer;
4969
if (require.main === module) {
5070
const server = createNicedayApiServer();
5171

72+
// schedule a tasks to regenerate the token every 9 hours
73+
setupTokenRegeneration();
74+
5275
server.listen(serverPort, () => {
5376
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
5477
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);

niceday-api/package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

niceday-api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"form-data": "^4.0.0",
2323
"isomorphic-fetch": "^3.0.0",
2424
"js-yaml": "^3.3.0",
25+
"node-schedule": "^2.1.1",
2526
"oas3-tools": "^2.2.3"
2627
},
2728
"devDependencies": {

niceday-api/service/MessagesService.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
const { Chat, SenseServerEnvironment, ConnectionStatus } = require('@sense-os/goalie-js');
1+
const {
2+
Authentication, Chat, SenseServer, SenseServerEnvironment, ConnectionStatus,
3+
} = require('@sense-os/goalie-js');
4+
5+
const { ENVIRONMENT, THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD } = process.env;
26

3-
const { ENVIRONMENT } = process.env;
47
let selectedServerEnv;
8+
let selectedServer;
59

610
if (ENVIRONMENT === 'dev') {
711
selectedServerEnv = SenseServerEnvironment.Alpha;
12+
selectedServer = SenseServer.Alpha;
813
} else {
914
selectedServerEnv = SenseServerEnvironment.Production;
15+
selectedServer = SenseServer.Production;
1016
}
11-
17+
const authSdk = new Authentication(selectedServer);
1218
/**
1319
* Send a text message
1420
*
@@ -22,7 +28,22 @@ exports.sendTextMessage = function (req, body) {
2228
const chatSdk = new Chat();
2329
chatSdk.init(selectedServerEnv);
2430
chatSdk.connect(req.app.get('therapistId'), req.app.get('token'))
25-
.catch((error) => reject(error));
31+
.catch(() => {
32+
// if the connection fails, we regenreate the token by logging in again
33+
// and we try to reconnect
34+
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
35+
.then((response) => {
36+
req.app.set('therapistId', response.user.id);
37+
req.app.set('token', response.token);
38+
chatSdk.connect(response.user.id, response.token)
39+
.catch((connectError) => {
40+
throw Error(`Error during chat connection: ${connectError}`);
41+
});
42+
})
43+
.catch((loginError) => {
44+
throw Error(`Error during authentication: ${loginError}`);
45+
});
46+
});
2647

2748
const subscriptionId = chatSdk.subscribeToConnectionStatusChanges((connectionStatus) => {
2849
if (connectionStatus === ConnectionStatus.Connected) {

niceday-broker/index.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const goalieJs = require('@sense-os/goalie-js');
2+
const schedule = require('node-schedule');
23

34
const {
45
Chat, Authentication, SenseServer, SenseServerEnvironment,
@@ -102,6 +103,27 @@ class MessageHandler {
102103
}
103104
}
104105

106+
function setupTokenRegeneration() {
107+
const rule = new schedule.RecurrenceRule();
108+
rule.hour = new schedule.Range(0, 23, 8);
109+
110+
const job = schedule.scheduleJob(rule, () => {
111+
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD)
112+
.then((response) => {
113+
chatSdk.init(selectedServerEnv);
114+
chatSdk.connect(response.user.id, response.token)
115+
.catch((connectError) => {
116+
throw Error(`Error during chat connection: ${connectError}`);
117+
});
118+
})
119+
.catch((error) => {
120+
throw Error(`Error during authentication: ${error}`);
121+
});
122+
});
123+
124+
return job;
125+
}
126+
105127
function setup(therapistId, token) {
106128
// Setup connection
107129
chatSdk.init(selectedServerEnv);
@@ -121,6 +143,11 @@ function setup(therapistId, token) {
121143
}
122144
module.exports.setup = setup;
123145

124-
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD).then((response) => {
125-
setup(response.user.id, response.token);
126-
});
146+
if (require.main === module) {
147+
authSdk.login(THERAPIST_EMAIL_ADDRESS, THERAPIST_PASSWORD).then((response) => {
148+
setup(response.user.id, response.token);
149+
});
150+
151+
// schedule a tasks to regenerate the token every 9 hours
152+
setupTokenRegeneration();
153+
}

0 commit comments

Comments
 (0)