-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (63 loc) · 1.78 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
63
64
65
66
67
68
69
70
71
const express = require('express');
const app = express();
const passport = require('passport');
const cookieSession = require('cookie-session');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const db = require('./models');
const { clientID, clientSecret } = require('./controllers/config/keys');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const PORT = process.env.PORT || 5000;
app.use(
cookieSession({
keys: ['asddsaasddsa'],
maxAge: 60 * 60 * 1000
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
db.User.findById(id).then(user => {
done(null, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID,
clientSecret,
callbackURL: '/auth/google/callback'
},
async (accessToken, refreshToken, profile, done) => {
const user = await db.User.findOne({ googleID: profile.id });
if (!user) {
const newUser = await db.User.create({
googleID: profile.id,
name: profile.displayName
// picture: profile.photos[0].value,
});
done(null, newUser);
} else {
done(null, user);
}
}
)
);
require('./routes/authRoutes')(app);
require('./routes/billingRoutes')(app);
require('./routes/surveyRoutes')(app);
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'ci') {
app.use(express.static('client/build'));
const path = require('path');
app.get('*', (req, res) => {
const prodPath = path.resolve(__dirname, 'client', 'build', 'index.html')
console.log(prodPath)
res.sendFile(prodPath);
});
}
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});