-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
111 lines (94 loc) · 2.97 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
const express = require("express");
const bodyparser = require("body-parser");
const session = require("express-session");
const path = require("path");
const passport = require("passport");
const { Strategy } = require("passport-local");
const mongoose = require("mongoose");
const connectMongo = require("connect-mongo");
const models = require("./models");
const routes = require("./routes");
const controllers = require("./controllers")
const PORT = process.env.PORT || 8080;
const app = express();
const User = require("./models/user")
const Score = require("./models/score")
// Configure express
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({ extended: true }))
const secrets = {
db: process.env.MONGODB_URI || "mongodb://localhost/thegamedb",
sessionSecret: 'secret'
}
// Database configuration with mongoose
const db = mongoose.connection;
mongoose.connect(secrets.db, (err, res) => {
if (err){
console.log('Error connecting to mongo')
} else {
console.log(`Mongoose connected to ${db.host}:${db.port}`)
}
});
// Configure passport for local auth
const local = new Strategy({
usernameField: "email",
passwordField: "password"
}, (email, password, done) => {
models.User.findOne({email}, (err, user) => {
if(!user) return done(null, false, { message: `Email ${email} not found` })
user.comparePassword(password, (err, isMatch) => {
if (isMatch) {
return done(null, user)
} else {
return done(null, false, { message: "Invalid email or password" })
}
})
})
})
// Configure Passport authenticated session persistence.
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. The
// typical implementation of this is as simple as supplying the user ID when
// serializing, and querying the user record by ID from the database when
// deserializing.
const MongoStore = connectMongo(session)
const sess = {
resave: true,
saveUninitialized: true,
secret: secrets.sessionSecret,
proxy: false,
name: "sessionId",
cookie: {
httpOnly: true,
secure: false
},
store: new MongoStore({
url: secrets.db,
autoReconnect: true
})
}
app.use(session(sess))
app.use(passport.initialize())
app.use(passport.session())
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user)
})
})
passport.use(local)
// Configure the routes
app.use('/auth', routes.auth);
app.use('/api', routes.api);
console.log("Serving static files from: " + path.join(__dirname,"client/build"))
app.use(express.static(path.join(__dirname,"client/build")));
app.get("*", function(req, res) {
console.log(req.url)
res.sendFile(path.join(__dirname, "/client/build/index.html"));
});
// Start the server
app.listen(PORT, function() {
console.log(`🌎 Server now listening on PORT ${PORT}!`);
});