-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (84 loc) · 2.78 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
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
const app = express();
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
const session = require('express-session')
const bcrypt=require('bcrypt')
const passport = require('passport')
const MongoStore = require('connect-mongo')(session);
const LocalStrategy=require('passport-local').Strategy;
const User = require('./models/User.js')
const Dog = require('./models/Dog.js')
const mongoose=require('mongoose');
// ℹ️ This function is getting exported from the config folder. It runs most middlewares
require("./config")(app);
// default value for title local
const projectName = "DoggoConnect";
const capitalized = (string) => string[0].toUpperCase() + string.slice(1).toLowerCase();
app.locals.title = `${capitalized(projectName)}`;
//SESSION CONFIG
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: false, // <== false if you don't want to save empty session object to the store
// cookie: {
// sameSite: 'none',
// httpOnly: true,
// maxAge: 60 * 1000 *60
// },
// store: new MongoStore({
// mongooseConnection: mongoose.connection,
//
// })
})
);
//defining strategy
passport.serializeUser((user, cb) => cb(null, user._id));
passport.deserializeUser((id, cb) => {
User.findById(id)
.then(user => cb(null, user))
.catch(err => cb(err));
});
passport.use(
new LocalStrategy((username, password, done) => {
// login
User.findOne({ username: username })
.then(userFromDB => {
if (userFromDB === null) {
// there is no user with this username
done(null, false, { message: 'Wrong Credentials' });
} else if (!bcrypt.compareSync(password, userFromDB.password)) {
// the password is not matching
done(null, false, { message: 'Wrong Credentials' });
} else {
// the userFromDB should now be logged in
done(null, userFromDB)
}
})
.catch(err => {
console.log(err);
})
})
)
app.use(passport.initialize());
app.use(passport.session());
// ℹ️ Connects to the database
// require("./db");
require("./db/index.js")
// 👇 Start handling routes here
const index = require("./routes/index");
app.use("/", index);
const auth = require("./routes/auth");
app.use("/", auth);
const private = require("./routes/private");
app.use("/", private);
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;