This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
62 lines (53 loc) · 2.1 KB
/
app.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
const express = require('express');
const session = require('express-session');
const healthgraphql = require('./src/healthgraphql');
const passport = require('passport');
const RunKeeperStrategy = require('passport-runkeeper').Strategy;
const cors = require('cors');
const RUNKEEPER_CLIENT_ID = process.env.RUNKEEPER_CLIENT_ID;
const RUNKEEPER_CLIENT_SECRET = process.env.RUNKEEPER_CLIENT_SECRET;
const COOKIE_SECRET = process.env.COOKIE_SECRET;
const PORT = process.env.PORT || 3000;
const SECURE = process.env.SECURE === 'true';
const HOSTNAME = process.env.HOSTNAME;
const PROXY = !RUNKEEPER_CLIENT_ID || !RUNKEEPER_CLIENT_SECRET || !COOKIE_SECRET;
const app = express();
if (!PROXY) {
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((obj, done) => done(null, obj));
passport.use(new RunKeeperStrategy({
clientID: RUNKEEPER_CLIENT_ID,
clientSecret: RUNKEEPER_CLIENT_SECRET,
callbackURL: `${SECURE ? 'https' : 'http' }://${HOSTNAME}:${PORT}/auth/callback`
},
(accessToken, refreshToken, profile, done) => done(null, {
access_token: accessToken,
id: profile.id
})
));
app.use(session({
secret: COOKIE_SECRET,
resave: false,
saveUninitialized: true,
cookie: {secure: SECURE}
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth', passport.authenticate('runkeeper'), () => {});
app.get('/auth/callback', passport.authenticate('runkeeper', {failureRedirect: '/auth/runkeeper'}), (req, res) => { res.redirect('/graphql'); });
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/');
});
} else {
app.use(cors());
}
app.use('/graphql', healthgraphql(
PROXY ? {} : {getAccessToken: req => req.user ? req.user.access_token : null})
);
app.listen(PORT, () => {
console.log(`Application started - ${SECURE ? 'https' : 'http' }://${HOSTNAME}:${PORT}/graphql`);
if (!PROXY) {
console.log(`Login at ${SECURE ? 'https' : 'http' }://${HOSTNAME}:${PORT}/auth`);
}
});