forked from viniciuscsr/cashapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (64 loc) · 1.89 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const pool = require('./db');
const sessions = require('client-sessions');
const users = require('./routes/users');
const money = require('./routes/money');
const flash = require('connect-flash');
const cookieParser = require('cookie-parser');
const session = require('express-session');
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.use('/public', express.static('public'));
app.use(cookieParser());
app.use(
session({
secret: 'flashSecret',
saveUninitialized: true,
resave: true,
})
);
app.use(flash());
// cookie settings for authentication sessions
app.use(
sessions({
cookieName: 'cashAppSession', // cookie name dictates the key name added to the request object
secret: 'cashAppSecret', // should be a large unguessable string
duration: 10 * 60 * 1000, // 30 min
httpOnly: false, //don't let JS code access cookies
// secure: true, // only set cookies over https
ephemeral: true, // destroy cookies when the browser closes
})
);
app.use((req, res, next) => {
if (!(req.cashAppSession && req.cashAppSession.userId)) {
return next();
}
pool.query(
'SELECT * FROM users WHERE id=$1',
[req.cashAppSession.userId],
(err, result) => {
if (err) {
return next(err);
}
if (!result.rows[0]) {
return next();
}
result.rows[0].password = undefined;
req.user = result.rows[0];
res.locals.user = result.rows[0];
next();
}
);
});
app.get('/', (req, res) => {
res.render('home', { sucess_message: req.flash('sucess')[0] });
});
app.use('/users', users);
app.use('/money', money);
app.listen(process.env.PORT || 3000, (err, res) => {
console.log('Server is running on port 3000');
});