-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
85 lines (74 loc) · 2.49 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
/*************************************
* The Social Hacker Blog
* Main Server
*************************************/
require('dotenv').config()
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const path = require('path');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const expressSanitizer = require('express-sanitizer');
// Initializing
const app = express();
// PORT connection
const PORT = process.env.PORT || 3000
/****************************************************
* SQL Connection
***************************************************/
const Sequelize = require('sequelize');
const sequelize = new Sequelize('socialhacker', process.env.DBUSER, null, { dialect: 'postgres', logging: false });
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err.message);
});
/****************************************************
* Check for login token on every request
***************************************************/
let verifyAuthentication = (req, res, next) => {
if (typeof req.cookies.jwtToken === 'undefined' || req.cookies.jwtToken === null) {
req.user = null;
} else {
var token = req.cookies.jwtToken;
//Synchronous verification
try{
decodedToken = jwt.verify(token, process.env.SECRETKEY);
//console.log("***Authenticate***");
req.user = decodedToken.id;
}catch(err){
console.log("Authentication Error:", err.message);
};
};
next();
};
let verifyUserLoggedIn = (req, res)=>{
if(!req.user){
res.redirect("/");
};
next();
};
/**************************************
* Middleware
***************************************/
app.use(express.static(__dirname));
app.use(express.static('./public'));
app.use(cookieParser())
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressSanitizer());
app.use(verifyAuthentication)
/*****************************************
* ROUTES LOAD
*****************************************/
require('./controlers/index.js')(app);
require('./controlers/user.js')(app);
/****************************************
* PORT LISTENER
****************************************/
app.listen(PORT, function() {
console.log('THE SOCIAL HACKER LISTENING ON PORT', PORT);
});