generated from NikkiHmltn/express_authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (63 loc) · 2.16 KB
/
server.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
require('dotenv').config();
const express = require('express');
const layouts = require('express-ejs-layouts');
const session = require('express-session');
const passport = require('./config/ppConfig');
const flash = require('connect-flash');
const SECRET_SESSION = process.env.SECRET_SESSION;
const app = express();
const methodOverride = require('method-override');
const db = require('./models');
// isLoggedIn middleware
const isLoggedIn = require('./middleware/isLoggedIn');
app.set('view engine', 'ejs');
app.use(require('morgan')('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
app.use(layouts);
app.use(methodOverride('_method'));
// secret: What we actually will be giving the user on our site as a session cookie
// resave: Save the session even if it's modified, make this false
// saveUninitialized: If we have a new session, we save it, therefore making that true
const sessionObject = {
secret: SECRET_SESSION,
resave: false,
saveUninitialized: true
}
app.use(session(sessionObject));
// Initialize passport and run through middleware
app.use(passport.initialize());
app.use(passport.session());
// Flash
// Using flash throughout app to send temp messages to user
app.use(flash());
// Messages that will be accessible to every view
app.use((req, res, next) => {
// Before every route, we will attach a user to res.local
res.locals.alerts = req.flash();
res.locals.currentUser = req.user;
next();
});
app.get('/', (req, res) => {
console.log(res.locals.alerts);
res.render('index', { alerts: res.locals.alerts });
});
app.get('/profile', isLoggedIn, (req, res) => {
db.storedChar.findAll({
where: {userId: req.user.id}
}).then((allCharacters)=>{
res.render('profile', {allCharacters});
}).catch((error) => {
res.status(400).render('main/404')
})
});
app.use('/auth', require('./routes/auth'));
app.use('/char', isLoggedIn, require('./routes/char'));
app.get('*', (req, res) => {
res.render('404')
})
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`🎧 You're listening to the smooth sounds of port ${PORT} 🎧`);
});
module.exports = server;