-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
147 lines (120 loc) · 4.13 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require("express"),
logger = require('morgan'),
expressHandlebars = require("express-handlebars"),
path = require('path'),
favicon = require('serve-favicon'),
bodyParser = require("body-parser"),
expressSession = require("express-session"),
flash = require("express-flash-messages"),
mongoose = require("mongoose"),
passport = require("passport"),
LocalStrategy = require("passport-local").Strategy;
mongoose.Promise = require("bluebird");
//==============================================================================
// Create app instance
const app = express();
//==============================================================================
const User = require(path.join(__dirname, 'models/user')),
keys = require(path.join(__dirname, 'config/keys'));
// Templates
const hbs = expressHandlebars.create({
extname: 'handlebars',
defaultLayout: "post_signin",
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true
}
});
// View Engine
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')))
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
/**
Middleware
*/
app.use(logger('dev'));
// Post Data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Session
app.use(
expressSession({
resave: false,
saveUninitialized: true,
secret:
process.env.SESSION_SEC || "You must generate a random session secret",
cookie: { maxAge: keys.cookieMaxAge } // 10 minutes (also cookie: { _expires: 60000 })
})
);
// Flash
app.use(flash());
// Connect to Mongoose
app.use((req, res, next) => {
if (mongoose.connection.readyState) next();
else {
const mongoUrl = process.env.MONGO_URL || keys.mongoURI;
mongoose
.connect(mongoUrl, { useNewUrlParser: true })
.then(() => next())
.catch(err => console.error(`Mongoose Error: ${err.stack}`));
}
});
// Passport
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(userId, done) {
User.findById(userId, (err, user) => done(err, user));
});
// Passport Local
const local = new LocalStrategy((username, password, done) => {
User.getAuthenticated(username, password, function(err, user, reason) {
if (err) done(err);
// login was successful if we have a user
if (user) {
// handle login success
console.log('login success');
done(null, user);
}
// otherwise we can determine why we failed
var reasons = User.failedLogin;
switch (reason) {
case reasons.NOT_FOUND:
case reasons.PASSWORD_INCORRECT:
// note: these cases are usually treated the same - don't tell
// the user *why* the login failed, only that it did
done(null, false, { message: "Invalid username/password, Try again!" });
break;
case reasons.MAX_ATTEMPTS:
// send email or otherwise notify user that account is
// temporarily locked
done(null, false, { message: "Your account is locked out, Try later!" });
break;
}
});
/*User.findOne({ username })
.then(user => {
if (!user || !user.validPassword(password)) {
done(null, false, { message: "Invalid username/password, Try again!" });
} else {
done(null, user);
}
})
.catch(e => done(e));*/
});
passport.use("local", local);
app.use((req, res, next) => {
res.locals.user = req.user;
next();
});
// Routes
app.use("/", require("./routes/router")(passport));
// Start Server
const port = process.env.PORT || 8080;
// Initialize a new socket.io object. It is bound to
// the express app, which allows them to coexist.
app.listen(port, () => console.log(`Server running on port ${port}`));