-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
124 lines (111 loc) · 3.49 KB
/
index.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
const express = require('express');
const path = require('path');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
const session = require('express-session');
const flash = require('connect-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const User = require('./models/user');
const productRoutes = require('./routes/products');
const authRoutes = require('./routes/auth');
const cartRoutes = require('./routes/cart');
const dashRoutes = require('./routes/dashboard');
const paymentRoutes = require('./routes/payment');
dotenv.config();
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(express.static(path.join(__dirname, '/public')));
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
});
// seed DB
// const seed = require('./seedDB');
// seed();
// session and flash messages config
const sessionConfig = {
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
};
app.use(session(sessionConfig));
app.use(flash());
// Inint the passport and sessions for storing the users
app.use(passport.initialize());
app.use(passport.session());
// Configuring the passport to use local strategy
passport.use(new LocalStrategy(User.authenticate()));
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENTID,
clientSecret: process.env.GOOGLE_CLIENTSECRET,
callbackURL: '/auth/google/redirect'
},
(accessToken, refreshToken, profile, done) => {
// passport callback function
//check if user already exists in our db with the given profile ID
User.findOne({
$or: [
{
username: profile._json.email
},
{
googleId: profile.id
}
]
}).then((currentUser) => {
if (currentUser) {
//if we already have a record with the given profile ID
done(null, currentUser);
} else {
//if not, create a new user
new User({
googleId: profile.id,
username: profile._json.email,
title: profile.displayName
})
.save()
.then((newUser) => {
done(null, newUser);
});
}
});
}
)
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id).then((user) => {
done(null, user);
});
});
app.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
res.locals.currentUser = req.user;
next();
});
// Routes
app.use(productRoutes);
app.use(authRoutes);
app.use(cartRoutes);
app.use(dashRoutes);
app.use(paymentRoutes);
app.get('/', (req, res) => {
res.redirect('/products');
});
app.all('*', (req, res) => {
res.render('404');
});
app.listen(process.env.PORT || 3000, () => {
console.log('Server running on port 3000.');
});