-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
69 lines (57 loc) · 2.19 KB
/
server.ts
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
import * as path from 'path';
import * as _ from 'lodash';
import * as moment from 'moment';
import * as express from 'express';
import * as passport from 'passport';
import { OfficeRnDStrategy } from './officeRnDStrategy';
import { OfficeRnDService } from './officeRnDService';
const app = express();
const cache = {
connected: false,
accessToken: null,
refreshToken: null,
expiresAt: null,
orgSlug: null
};
function updateCache(access_token, refresh_token, expires_in, org_slug) {
const expiresAt = moment().add(expires_in, 'seconds');
cache.accessToken = access_token;
cache.refreshToken = refresh_token;
cache.expiresAt = expiresAt.format('MMMM Do YYYY, h:mm:ss a');
cache.connected = expiresAt.isAfter();
cache.orgSlug = org_slug || cache.orgSlug;
}
function officeRnDLoggedIn(req, access_token, refresh_token, { expires_in }, profile, done) {
updateCache(access_token, refresh_token, expires_in, req.query.org_slug);
done(null, {});
}
passport.use(new OfficeRnDStrategy(officeRnDLoggedIn));
app.use(passport.initialize());
app.get('/connect', passport.authorize('officernd'));
app.get('/connect/return', passport.authorize('officernd'), (req, res) => res.sendFile(path.join(__dirname + '/views/authReturn.html')));
app.get('/', (req, res) => res.sendFile(path.join(__dirname + '/views/index.html')));
app.get('/status', (req, res) => {
res.send(cache);
});
app.get('/refresh', async (req, res) => {
const service = new OfficeRnDService();
const { accessToken, refreshToken, expiresIn } = await service.refreshTokens(cache.refreshToken);
updateCache(accessToken, refreshToken, expiresIn, null);
res.send();
});
app.get('/members', async (req, res) => {
const service = new OfficeRnDService();
const members = await service.getMembers(cache.orgSlug, cache.accessToken);
res.send(members);
});
app.use(handleError);
app.listen(3000, () => console.log(`Demo oauth app listening on port 3000!`));
function handleError(err, req, res, next) {
console.log(err);
res.status(500).send({
error_message: err.message
});
}
process.on('uncaughtException', (err) => {
console.error(`Uncaught Exception. Error: ${err}.`, err);
});