-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
226 lines (196 loc) · 5.28 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require("dotenv").config();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const express = require("express");
const favicon = require("serve-favicon");
const hbs = require("hbs");
const mongoose = require("mongoose");
const logger = require("morgan");
const path = require("path");
//session
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
//msg
const flash = require("connect-flash");
//auth
const passport = require("passport");
const LocalStrategy = require("passport-local");
const GoogleStrategy = require("passport-google-oauth2").Strategy;
//const FacebookStrategy = require("passport-facebook").Strategy;
const User = require("./models/User");
const bcrypt = require("bcrypt");
/* -------------------------------------------- */
hbs.registerHelper("json", (context) => {
return JSON.parse(context)
})
// CONNECT TO DATABASE
mongoose
.connect(
process.env.MONGODB_URI ||
`mongodb://${process.env.DATABASE_CONNECTION}/NetflixSearch`, {
useNewUrlParser: true
}
)
.then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch(err => {
console.error("Error connecting to mongo", err);
});
/* -------------------------------------------- */
// EXPRESS APP SETUP
const app_name = require("./package.json").name;
const debug = require("debug")(
`${app_name}:${path.basename(__filename).split(".")[0]}`
);
const app = express();
/* -------------------------------------------- */
// MIDDLEWARE SETUP
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(cookieParser());
app.use(
session({
secret: process.env.SESSION_SECRET,
cookie: {
maxAge: 24 * 60 * 60 * 1000,
},
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: mongoose.connection
})
})
);
app.use(passport.initialize());
app.use(passport.session());
/* -------------------------------------------- */
// EXPRESS VIEW ENGINE SETUP
app.use(
require("node-sass-middleware")({
src: path.join(__dirname, "public"),
dest: path.join(__dirname, "public"),
sourceMap: true
})
);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
app.use(express.static(path.join(__dirname, "public")));
app.use(favicon(path.join(__dirname, "public", "images", "favicon.ico")));
/* -------------------------------------------- */
// PASSPORT SETUP
// serialize
passport.serializeUser((user, done) => {
// store user id in session
done(null, user._id);
});
// deserialize
passport.deserializeUser((id, done) => {
User.findById(id)
.then(user => {
done(null, user);
})
.catch(err => {
done(err);
});
});
// local strategy --> check for match in db
passport.use(
new LocalStrategy((username, password, done) => {
User.findOne({
username
})
.then(user => {
// no username match
if (!user) {
done(null, false, {
message: "Invalid credentials - try again"
});
return;
}
// username match
bcrypt.compare(password, user.password).then(bool => {
// no password match
if (!bool) {
done(null, false, {
message: "Invalid credentials - try again"
});
return;
}
// password match
done(null, user);
});
})
.catch(err => {
done(err);
});
})
);
// facebook strategy --> auth with passport
// passport.use(
// new FacebookStrategy({
// clientID: process.env.FACEBOOK_APP_ID,
// clientSecret: process.env.FACEBOOK_APP_SECRET,
// callbackURL: "http://localhost:3000/auth/facebook/callback"
// },
// function (profile, cb) {
// User.findOrCreate({
// facebookId: profile.id
// },
// function (err, user) {
// return cb(err, user);
// }
// );
// }
// )
// );
// google strategy --> auth with passport
passport.use(
new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: `http://${process.env.GOOGLEURL}/auth/google/callback`
},
(request, accessToken, refreshToken, profile, done) => {
User.findOne({
googleId: profile.id
})
// if user found
.then(user => {
if (user) {
// log in user
done(null, user);
} else {
// create as new user
User.create({
googleId: profile.id,
username: profile.name.givenName
}).then(createdUser => {
// log the new user in
done(null, createdUser);
});
}
})
.catch(err => {
done(err);
});
}
)
);
/* -------------------------------------------- */
// REGISTER ROUTES
const index = require("./routes/index");
app.use("/", index);
const authRoutes = require("./routes/auth");
app.use("/auth", authRoutes);
/* -------------------------------------------- */
// default value for title local
//app.locals.title = "Netflix Movie Search";
module.exports = app;