-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
169 lines (146 loc) · 3.86 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
require("dotenv").config();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const express = require("express");
const hbs = require("hbs");
const mongoose = require("mongoose");
const path = require("path");
const User = require("./models/user");
const Place = require("./models/place");
const session = require("express-session");
const MongoStore = require("connect-mongo")(session);
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
const flash = require("connect-flash");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch(err => {
console.error("Error connecting to mongo", err);
});
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(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Express View engine setup
app.set("view engine", "hbs");
app.set("views", path.join(__dirname, "/views"));
app.use(express.static(path.join(__dirname, "public")));
//Session middleware
app.use(
session({
secret: "project2",
cookie: {
maxAge: 1200000
},
store: new MongoStore({
mongooseConnection: mongoose.connection,
ttl: 24 * 60 * 60
}),
resave: true,
saveUninitialized: true
})
);
// PASSPORT CONFIG
// cookie
passport.serializeUser((user, cb) => {
cb(null, user._id);
});
// session
passport.deserializeUser((id, cb) => {
User.findById(id, (err, user) => {
if (err) {
return cb(err);
}
cb(null, user);
});
});
// initialising passport
app.use(passport.initialize());
app.use(passport.session());
// adding error handling flash
app.use(flash());
// passport local strategy config
passport.use(
new LocalStrategy(
{
passReqToCallback: true
},
(req, username, password, next) => {
User.findOne(
{
username
},
(err, user) => {
if (err) {
return next(err);
}
if (!user) {
return next(null, false, {
message: "Incorrect username"
});
}
if (!bcrypt.compareSync(password, user.password)) {
return next(null, false, {
message: "Incorrect password"
});
}
// success - user is logged in!
app.locals.user = user;
return next(null, user);
}
);
}
)
);
//GOOGLE STRATEGY
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/auth/google/callback"
},
(accessToken, refreshToken, profile, done) => {
// to see the structure of the data in received response:
console.log("Google account details:", profile);
User.findOne({ googleID: profile.id })
.then(user => {
if (user) {
done(null, user);
return;
}
User.create({ googleID: profile.id, username: profile.displayName})
.then(newUser => {
done(null, newUser);
})
.catch(err => done(err)); // closes User.create()
})
.catch(err => done(err)); // closes User.findOne()
}
)
);
//ROUTES
const index = require("./routes/index");
app.use("/", index);
const auth = require('./routes/auth');
app.use('/', auth);
//CHECKING PORT
app.listen(process.env.PORT, () =>
console.log(`rodando na porta ${process.env.PORT}`)
);
module.exports = app;