-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
115 lines (102 loc) · 2.82 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
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const bodyParser = require('body-parser');
const winston = require('winston');
const expressWinston = require('express-winston');
const config = require('./config');
const smoochApi = require('./smoochApi');
const { extractAppId } = require('./tokenUtils');
const app = express();
app.disable('x-powered-by');
app.set('views', 'views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(expressLayouts);
app.use(bodyParser.json());
app.use((req, res, next) => {
res.renderMain = (view, props) => {
props = props || {};
props.layout = 'main';
res.render(view, props);
};
next();
});
app.use(
expressWinston.logger({
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.json()
),
expressFormat: true,
})
);
app.get('/', (req, res) => {
res.renderMain('addToSunshineConversations', config);
});
app.get('/oauth', (req, res) => {
if (req.query.error) {
return res.renderMain('error', {
error: req.query.error,
});
} else {
const props = config;
props.code = req.query.code;
return res.renderMain('exchangeToken', props);
}
});
/**
* Exchanges an authorization code, yields an access token.
*/
function exchangeCode(code) {
const { suncoBaseUrl, oauthClientId, oauthClientSecret } = config;
return fetch(`${suncoBaseUrl}/oauth/token`, {
method: 'POST',
body: new URLSearchParams({
code,
grant_type: 'authorization_code',
client_id: oauthClientId,
client_secret: oauthClientSecret,
}),
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(({ access_token }) => access_token);
}
app.get('/exchange', (req, res) => {
let token;
exchangeCode(req.query.code)
.then((exchangedCode) => {
token = exchangedCode;
return smoochApi.sendTestMessage(token);
})
.then((appUser) => {
const appId = extractAppId(token);
console.log(`Integration with App ID ${appId} successful!`);
res.renderMain('success', {
appUser,
url: `${config.suncoBaseUrl}/apps/${appId}/${config.oauthClientId}`,
});
})
.catch((err) => {
console.error(err);
res.status(500).send('An unexpected error occurred');
});
});
app.get('/settings', (req, res) => {
res.renderMain('settings');
});
app.post('/remove', (req, res) => {
console.log(
`The integration with ID ${req.body.integrationId} has been been removed!`
);
res.end();
});
app.listen(process.env.PORT || 3000);
console.log(`App listening on ${process.env.PORT || 3000}`);